Place string based on two other columns in power query - powerbi

I'm trying to replace a value with an string when a condition is met in two columns.
Column 1
Column 2
Column 3
A
1
Step 1 - Description for step one for A
B
2
Step 2 - Description for step one for B
C
1
Step 1 - Description for step one for C
C
3
Step 3 - Description for step one for C
A
1
Step 1 - Description for step one for A
B
3
Step 3 - Description for step one for B
I tried the code
= Table.ReplaceValue(
#"Renamed Columns",
each [Column 3],
each if [Column 1] = "A" and [Column 2] = "1" then "Step 1 - Description for step one for A" else [NULL])
where I was planning to iterate this code for all the combinations of column 1 and 2 (where each combination gets a certain string in column 3).
How would I be able to get this done? Thanks in advance!

I would definitely create and merge a table, but if you want to do it this way
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns", each [Column3], each if [Column2]=1 and [Column1]="A" then "Step 1 - Description for step one for A" else null ,Replacer.ReplaceText,{"Column3"})
note, you have to do else null not else [NULL] unless you really have that column name. else NULL would not work either since powerquery is case sensitive
Also check if you mean to use Column 1 or Column1 the difference being a space between the n and 1

Related

Informatica : Count the number of rows based on sequence of number

I have table where source has 1 column, like below. for example, column name is A and I have set of records in the source.
A
1
1
1
2
2
3
I want to populate two columns in target, Say columns are A and B.
Column A in the Target has same values as in source and column B has count
A B
1 1
1 2
1 3
2 1
2 2
3 1
Can someone please explain how can i achieve this.
Thanks in advance
If source is a dbms like oracle, you can use source qualifier overwrite sql like below. Use row number and partition by to generate sequence for every A.
Select
A, row_number() over(partition by A order by A) as B
From mytable
If you're looking for infomatica only solution then this is how you can do it.
Sort the data by column A
Use ex transformation, create one in/out two var, and one out port.
We are going to compare first val with prev val, if they r same, add 1to the sequence else start from 1 again.
A in/out
v_B = iif (A=prev_A, v_B +1, 1)
prev_A=A
o_B =v_B
Link A and o_B to the target.

How to loop through values in a column in DAX

I have a table that looks like this:
Document PartNum Cost
A 1 5
A 1 5
A 2 3
A 2 3
B 1 1
B 1 1
B 3 4
B 3 4
I am trying to get the SUM of the cost for each part (counted once) on each document. So my new column would look like this:
Document PartNum Cost NewColumn
A 1 5 8
A 1 5 8
A 2 3 8
A 2 3 8
B 1 1 5
B 1 1 5
B 3 4 5
B 3 4 5
In R I would use a for loop, max and an append function of some kind. I am struggling to find a solution using DAX in PowerBI. Thank you in advance!
I have a solution using MQuery & DAX.
#"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"PartNum", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type1", "DocPart", each [Document]&[PartNum]),
#"Removed Duplicates" = Table.Distinct(#"Added Custom", {"DocPart"})
Step 1: Make PartNumber type text
Step 2: Create new custom column by concatenation of Document & PartNumber.
Step 3: Remove duplicates from New Custom Column.
Step 4: Now close N apply.
Step 5: create a new Measure using dax:
TotalCost = CALCULATE(SUM(uni[Cost]),ALL(uni[DocPart],uni[PartNumber],uni[Cost]))
Step 6: Drag required columns.
Note: Please refer screenshot for output data before applying code

DAX Power BI Conditional filtering

Sample file
The column c of a table T can have the values 1, 2 or 3. I want to filter the table such as when the user selects value 1 nothing is filtered. When the selected value is 2 then show only the rows with the c column containing the value 2 or the value 3 not the value 1 and finally if the selected value is 3 then show only those rows containing 3 in the c column. The slider or a plain filter must be single selection not multi because otherwise it would violate one of the user's business rule.
Selected Show
1 all rows
2 rows with 2 or 3
3 only rows with 3
I tried to create columns and to create measures but I can't get anywhere. Any directions?
I agree that the disconnected table might be the best workaround here. I hope this solution will work with your actual file.
Step 1 - create a new disconnected table
FilterC = DISTINCT(T[c])
Step 2 - make sure that your slicer is sourced from the new table (FilterC)
Step 3 - create a measure that will return 1 for rows that we want to see, and 0 for rows that we want to hide:
mFilter =
var Filter_C = SELECTEDVALUE(FilterC[c])
var Row_C = SELECTEDVALUE(T[c])
return
SWITCH(
Filter_C,
"1", 1,
"2", IF(OR(Row_C = "2", Row_C = "3"), 1, 0),
"3", IF(Row_C = "3", 1, 0)
)
Step 4 - add this measure to the table, or even table filters will suffice.
Step 5 - start switching the values!
First you need to set it to a number column.
Second, you could write complex measures and use a disconnected table, but the best option is to use the correct slicer. There is a Greater than or Equal To option.

How to incorporate thresholds for limit check with a provided static value to compare against?

here is the instance:
Column 1 Column 2 Column 3
2.99 4 Price OK
1.99 4 Price below limit
12.99
5.99 6 Price OK
1.99 6 Price below limit
8.99 6 Price OK
So for Power BI context Column 2 is a custom column from power query, the goal is to set a threshold value for column 2 pack size, in this instance pack size of 4 needs to check for minimum price of $2.99 (higher is ok), below the price should be below limit, in instance of column 2 blanks (result should also be blank). In the instance of size 6 the minimum price to check for is 5.99.
Is there a decent way to go about this?
Let's do this in two steps. First, create a column MinPrice that defines your minimum prices.
if [Column 2] = 4 then 2.99
else if [Column 2] = 6 then 5.99
else null
Then create a column that compares the actual and the minimal
if [Column 1] = null or [Column 2] = null then null
else if [Column 1] < [MinPrice] then "Price below limit"
else "Price OK"
If you have a bunch of unique values in Column 2 that you need to create rules for, then create a reference table that you can merge onto your original table and expand the MinPrice column instead of the first step stated above.
Column2 MinPrice
-----------------
4 2.99
6 5.99
8 7.99
...

Generating a List in M Query

I am trying to generate a list using M Query, but instead of generating a list starting from a specific, number, I want the number to start at 1 and then add 1 for every row on another table.
So for example, if I have:
Tbl1
Col1
A
B
C
D
D
I want to generate
Tbl2
Col1
1
2
3
4
5
I want the number to start at 1 and then add 1 for every row on another table.
if I understand your correctly, here it is:
={1..Table.RowCount(Tab1)}