Traslate formula from Excel to DAX( PowerBI) - excel

hope everything is going well for everyone
Look, I have this table and the following excel formula to create a Key for each row on the table.
=CONCAT(IF(LENGTH(Loc.)=1;0;""); Loc.;EXTRACT(Nombre Institución Educativa;SEARCH(" "; Nombre Institución Educativa)+1;3);RIGHT(DANE 12;5);"-";IF(LENGTH(Sitio de Entrega)=1;0;""); Sitio de Entrega)
enter image description here
I need to traslate this formula to DAX (PowerBI), to create a new column called Key1, while import the data from a database. I'm a bit new on this though and I don't know how can I make it works the length, extract and search functions from Excel on DAX.
Thanks in regards if somebody know the answer and can help me

Key1 =
FORMAT ( 'MyTable'[Loc.], "0#" )
& MID (
'MyTable'[Nombre Institución Educativa],
SEARCH ( " ", 'MyTable'[Nombre Institución Educativa] ) + 1,
3
)
& RIGHT ( 'MyTable'[Dane 12], 5 ) & "-"
& FORMAT ( 'MyTable'[Sitio de Entrega], "0#" )
Replace 'MyTable' with your actual table name. The formula does the following:
Pads Loc. with a 0
Gets the first 3 characters after the space in Nombre Institución Educativa
Gets the last 5 characters of Dane 12
Adds a - and pads Sitio de Entrega with a 0
The ampersand concatenates the string

Related

Use Table Header as Content In a Cell

I am using the column header titles as the comma separated content in another cell. I am using Excel 2016. I have a table named StudentCourse and for a better illustration please see the below example layout:
[Name] [Math] [Geo] [Bio] [Fees] [Fixes]
Ram Very Bad Good Good Unpaid Urgent: Math, Fees
Dam Neutral Good Bad Paid Urgent: Math, Bio
Rik Good Good Good Paid OK: Not Urgent
Nik Good Good Good Partial Urgent: Fees
The values for the subject columns are from a drop down menu which has the options Good, Neutral, Bad and Very Bad and if the values Neutral, Bad or Very Bad are selected then the Fixes column will be updated with the prefix Urgent: and the column header name (Math, Geo or Fees) depending on what needs to be fixed. If, no fixes are needed then the Fixes column's value will be Ok: Not Urgent.
The Fees column also follows the same concept. Meaning that if the Partial (means partial payment) or unpaid dropdown options are selected for the Fees Column value, then the Fees will be added to the Fixes column. So in short the Fixes column is for easily sorting through what needs to be given special by having the values be automatically selected based on what was chosen for the other columns.
I should also mention that I am new to Excel.
Assuming that the table is located at [A1:E9] and there are no [BLANK] cells as confirmed by OP. Enter this formula in [F2] and copy it to [F3:F9].
Excel 2016
= IF( SUMPRODUCT( ($B2:$E2<>{"Good","Good","Good","Paid"})*1 )=0, "Ok: Not Urgent",
"Urgent: " & SUBSTITUTE(
IF( $B2<>"Good", ", " & $B$1, "" )
& IF( $C2<>"Good", ", " & $C$1, "" )
& IF( $D2<>"Good", ", " & $D$1, "" )
& IF( $E2<>"Paid", ", " & $E$1, "" ), ", ", "", 1 ) )
Excel 2019 (Formula Array)
= IF( SUMPRODUCT( ($B2:$E2<>{"Good","Good","Good","Paid"})*1 )=0, "Ok: Not Urgent",
"Urgent: " &
TEXTJOIN( ", ", TRUE, IF( ($B2:$E2<>{"Good","Good","Good","Paid"}), $B$1:$E$1, TEXT(,) ) ) )
The FormulaArray is entered holding down ctrl+shift+enter simultaneously, the formula would be wrapped within { and } if entered correctly.
If you list the acceptable data in column H:I (as example below). You could use:
=IF(TEXTJOIN(", ",1,IF(INDEX($I$1:$I$4,MATCH($B$1:$E$1,$H$1:$H$4,0))=B2:E2,"",$B$1:$E$1))="","OK: No urgent","Urgent: "&TEXTJOIN(", ",1,IF(INDEX($I$1:$I$4,MATCH($B$1:$E$1,$H$1:$H$4,0))=B2:E2,"",$B$1:$E$1)))

Excel - Is there a way to tell a cell to use a certain function based on the first letter of a another cell?

I am creating a Product Decoder for a project.
Lets say, Our product can have a code such as "ABCDE" OR a code like "BCDEF".
ABCDE has a table of data that I use to decode using a lookup. For example AB can decode into "Potato" and CDE can decode into "Chip". So any combination with AB can be Potato "Anything else".
BCDER, BC can decode into "Veggie" so DER can code into "Chip".
I also use the 1/search method to take placements for the decode. Example =IFERROR(LOOKUP(2,1/SEARCH($E$19:$E$23,N18),$E$19:$E$23), "")
I concatenate all the decodes using =S2&" "&T2&" "&U2&" "&V2
Question is...if we are getting a huge amount of product code coming that I want to decode into one single column... How do I tell excel to use this table of data for ABCDE if product starts with "A", if not, use table of data that correlates to BCDER when product starts with "B".
Edit 1:
Here is my table, right side is where i look up the Part Number column N"
As you can see on column "W" I concatenate the date is Look up from columns O~V.
Column O Function: =IFERROR(LOOKUP(2,1/SEARCH($C$1:$C$7,N2),$C$1:$C$7), "")
On column N, I have two parts. One that starts with M and one that starts with K which is pretty standard.
Image two is me trying to use the IF Left but, it doesn't really work
=IF(LEFT(AA4,10) = "M ", W2, W18)
So How can I tell my excel page to use Table A1:A12 if part starts with "M*" and vice versa?
Let me know if this is confusing, I will do my best to clear things up.
First, a possible correction
I think this function does not give you what you say it does:
= IFERROR(LOOKUP(2,1/SEARCH($E$19:$E$23,N18),$E$19:$E$23), "")
You might mean:
= IFERROR( LOOKUP( 2, 1/SEARCH( $E$19:$E$23, N18 ), $F$19:$F$23 ), "" )
Because you want to look up the value in column E and return the value in column F. If that's not true, then skip the rest of this answer.
Now the solution
What you're trying to do is change the lookup array if the part number starts with a different letter. So, the IF( LEFT( combo mentioned by #BigBen should be used to modify the lookup array. I think it would look like this:
= IFERROR( LOOKUP( 2
,1/SEARCH( IF( LEFT( AA4, 1 ) = "M"
,$C$2:$C$12
,$C$19:$C$23 )
,N2 )
,IF( LEFT( AA4, 1 ) = "M"
,$D$2:$D$12
,$D$19:$D$23 )
)
,"")

How to reverse comma separated string in Excel?

How can I use a formula in MS Excel to reverse a comma separated sting in row? The number of values are not always same so some rows have 3, 4 and some only one element.
So the output looks like following image
If you have Office 365 Excel then use This array formula:
=TEXTJOIN(",",,TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",99)),((LEN(A2)-LEN(SUBSTITUTE(A2,",",""))+1)-ROW($XFD$1:INDEX(XFD:XFD,LEN(A2)-LEN(SUBSTITUTE(A2,",",""))+1)))*99+1,99)))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
If one does not have Office 365 Excel then vba will probably be the only choice.
Sloppy UDF solution:
Function REVERSESTRING(original As Range, delim As String)
Dim i As Long, reversed As String, arr As Variant
arr = Split(original.Value, delim)
For i = UBound(arr) To 0 Step -1
reversed = reversed & arr(i) & ","
Next i
REVERSESTRING = Left(reversed, Len(reversed) - 1)
End Function
A pure single-cell formula works in Excel 365; with that version there is no need for special array calculation entry, VBscripts, or helper columns:
Procedure
Make the following table in Excel:
Add column headings
Select headings, and press the Table icon on the Insert ribbon tab
Copy formula 1 & 2 below into the appropriate columns
Original | # | Reversed
---------+-----+-----------
| {1} | {2}
Place the string you would like to reverse in the first column.
Details and customization
'Original' column
(holds the delimited string value you wish to process)
Place your source data in this column (as seen in the example images)
'#' column
(counts number of items in delimited cell)
Note: (this column is completely optional; it simply shows the number of items in the original column)
{1} <- replace with formula #1:
=LET(
existingDelimiter, ","
, originalValue, [#[Original]]
, SUM(
LEN(originalValue)
- LEN(
SUBSTITUTE(
originalValue
, existingDelimiter
, ""
)
)
)
+1
)
(Note: If your list uses a delimiter other than a comma or if the first column of your table will have a different name, edit the above as appropriate using the same instructions given for formula #2 below.)
'Reversed' column
(reverses order of delimited items in list; optionally changes delimiter)
{2} <- replace with formula #2:
=LET(
existingDelimiter, ","
, newDelimiter, ","
, originalValue, [#[Original]]
, SUBSTITUTE(
ARRAYTOTEXT(
LET(
list,
IFERROR(
FILTERXML(
"<t><s>"
& SUBSTITUTE(
originalValue
,existingDelimiter
,"</s><s>"
)
& "</s></t>"
, "//s"
)
,""
)
,SORTBY(
list,
SEQUENCE(
ROWS(list)
,1
,ROWS(list)
,-1
)
)
)
,0
)
, ", "
, newDelimiter
)
)
Adjust the formula for what you are trying to accomplish by changing the values for existingDelimiter, newDelimiter, and originalValue, as necessary.
A. To reverse a comma-separated string, use the formula as written:
existingDelimiter, ","
, newDelimiter, ","
Example:
B. To reverse DNS names, replace the comma with a period in the definitions for existingDelimiter and newDelimiter:
existingDelimiter, "."
, newDelimiter, "."
This can be very useful for reverse DNS names (aka Java class names / Universal Type Indicators (UTIs) / etc.)
Example:
Replace "Original" in [#[Original]] with the name of your first column, if different.
A. If using just a single cell for input instead of a table column, replace [#[Original]] with the reference to that cell (e.g. B2):
, originalValue, B2
Example:
Explaination of the "Reversed" column formula:
By manually converting to XML, we can use the FilterXML function, which converts the data to an array.
Having the data in an array allows the use of the SortBy function.
SortBy reverses the array by using a helper array created with the Sequence function.
Finally, the ArrayToText functions converts this (now reverse-ordered) array back to a text string that will fit in a single spreadsheet cell.
This is what allows us not to need a loop, helper columns, or VBScript.
Bonus column
To extract a specific term from a list, use the following formula in another table column:
(Change the number in termNumber to the desired value):
=LET(
existingDelimiter, ","
, originalValue, [#[Original]]
, termNumber, "[2]"
, IFERROR(
FILTERXML(
"<t><s>"
& SUBSTITUTE(
originalValue
, existingDelimiter
, "</s><s>"
)
& "</s></t>"
, "//s"
& termNumber
)
, ""
)
)
Example:
Other notes
Needed:
Excel 365 (for at least FilterXML and Let functions, and dynamic arrays) (*)
It might work with other versions but I have not tested those. Please note in the comments if you notice other or future versions (e.g. Excel 2022) work with this.
Not needed:
dynamic array entry
VBscript
macro-enabled files
(*) This can be done without the Let function, but using Let allows the calculation to be edited / repurposed with less chance for user error.
Note: When the Lambda function is released (hopefully later in 2021) then this all can be wrapped up in a named worksheet function
Bonus: to edit more complex Excel formulas with code highlighting (and other features such as auto-intents and folding), try a text editor with Swift programming language support. Examples:
VSCode editor (free; cross-platform)
Until there is an Excel-specific extension available, install a Swift VSCode extension such as this: Swift language VSCode extension (it seems to work quite well to provide code highlighting and folding)
Notepad++ (free; Windows-only)
Select "Swift" from the "Language" menu
Inspired by (apologies if I've forgotten someone):
https://www.howtoexcel.org/tutorials/split-text-by-delimiter/
https://www.sumproduct.com/news/article/have-you-got-what-it-text-introducing-arraytotext-and-valuetotext
https://exceljet.net/formula/reverse-a-list-or-range

Split prefix and surname (based on array data)

I have a column of data existing out of names, which may or may not contain a surname prefix. Those prefixes can exist out of multiple words. I have a list of all possible prefixes, but now I need to split the prefix and surname and make 2 columns with the data.
What I did was writing an excel formula like the following:
=IF(
RIGHT(A1;7) = " van de"
;
RIGHT(A1;6)
;
IF(
RIGHT(A1;4) = " van"
;
RIGHT(A1;3)
;
IF(
RIGHT(A1;3) = " de"
;
RIGHT(A1;2)
;
--Insert more nested If statements here--
)
)
)
Data of the surnames can look like the following:
Name1 van de
Name1 van
Name1
Name1 Name2 van
Name1-Name2 Name3 van de
Name1 Name2 Name3
What I want:
OriginalName | Name | Prefix
-----------------|--------|----------
a b | a | b
a b c | a | b c
Firstly this is a pretty inefficient method, but I automated the creating of this formula, so that wasn't a problem anymore. Now I found out there's a limit to the nested If statements one can have, and I have to exceed that limit.
How should I solve this problem?
I have an array with the possible prefixes. Maybe this will help?
I made the assumption that you wanted to separate the "van" and "de" prefixes from the rest of the name. If I misunderstood, please provide more examples of your problem/question...
The following solution requires a helper column to determine where the "Prefix" starts, but you can hide it if necessary:
First, put my values in A8:A9 (van; de) anywhere and name it prefix so it can be referenced in the following formulas.
The formula in C1 is an array formula (use Ctrl+Shift+Enter):
=MIN(IF(ISNUMBER(SEARCH(prefix,A1)),SEARCH(prefix,A1)))
The formula in D1 and E1 or normal formulas:
=IF(C1>0,LEFT(A1,C1-2),A1)
=IF(C1>0,MID(A1,C1,LEN(A1)),"")
Put your list in order of the longest surname to the shortest. I put mine in E1:E3.
Then use this array formula:
=TRIM(IFERROR(SUBSTITUTE(A1,INDEX($E$1:$E$3,MATCH(TRUE,ISNUMBER(SEARCH($E$1:$E$3,A1)),0)),""),A1))
Then to get the Surname:
=IFERROR(INDEX($E$1:$E$3,MATCH(TRUE,ISNUMBER(SEARCH($E$1:$E$3,A1)),0)),"")
Being an array formulas they need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
I know this has already been answered but I done this yesterday and didn't have time to submit it (had to run for the bus).
All Prefix values have a space before them...
Column B formula (Array formula - Ctrl+Shift+Enter in formula bar)
=INDEX(SUBSTITUTE(A1,Prefix,""),MATCH(SMALL(LEN(SUBSTITUTE(A1,Prefix,"")),1),LEN(SUBSTITUTE(A1,Prefix,"")),0))
Column C formula - =TRIM(SUBSTITUTE(A1,B1,""))

Excel Formula with concatenate

I am trying to generate a customer number using the first three letters of the customers last name, the first name initial and middle initial, followed by the last four of their phone number. How would I do this? All I need is the formula.
First_Name Middle_Initial Last_Name Street_Address City State Zip Phone
Nathaniel E. Conn 6196 View Ct Lancing TN 37770 567-273-3956
Something like this (assuming a table with [structured-references], fill in the actual cell names if not):
=LEFT([LastName] & "---", 3)
& LEFT([FirstName] & "-", 1)
& LEFT([MiddleInitial] & "-", 1)
& RIGHT([PhoneNumber] & "----", 4)
I have used dashes ("-") to fill in any spaces where the field might be smaller than the number of characters you need from it. You can change them to any fill character that suits you.
Well, it depends on if each piece of data has its own column, looks like it does.
You can use the left/right functions to parse the data out of your columns.
=CONCATENATE(RIGHT(C1,3) & LEFT(A1,1) & LEFT(B3,1) & RIGHT(H1,4))
I would do:
=MID(CELL_LAST_NAME;1;3)&MID(CELL_FIRST_NAME;1;1)&MID(CELL_MIDDLE_NAME;1;1)&MID(CELL_PHONE;LEN(CELL_PHONE)-3;4)

Resources