So I have this logic from crystal reports formula builder that I'd like to follow into powerBI's DAX and create a new column with the result:
if {tableA.Col1} = "PAY" and {tableB.Col#} in ["LEG", "HAND"] and {tableA.Col2} = "Y" then "N" else "Y"
I cant seem to create any logical statement to refer to another table within DAX (up to my knowledge) or actually nest multiple ifs like above, here is what I have so far..
Error = IF(tableA[col1] = "PAY" && IF(tableB[col#] = "LEG"&&"HAND" && IF(tableA[col2] = "Y","N","Y"),"Y"),"Y")
Error Column =
IF (
tableA[col1] = "PAY"
&& RELATED ( tableB[col#] )
IN { "HANDS", "LEGS" }
&& tableA[col2] = "Y",
"N",
"Y"
)
Related
I have a table of the customers check out funnel through an app and a table of total sales. I want to find the number of sales that can be attributed to the app. I think the easiest way to do this is to concatenate the name and barcode in both of these tables. Then I can see if the values column, NameVIN, for the sales table appear in NameVIN for the checkout funnel table.
For obvious reasons I will not be sharing data for this question.
NameVIN =
if(
ConsumerFunnelTime[CustomerFirstName] <> BLANK() && ConsumerFunnelTime[CustomerLastName] <> BLANK(), ConsumerFunnelTime[CustomerFirstName] & ConsumerFunnelTime[CustomerLastName] & ConsumerFunnelTime[VIN],BLANK())
NameVIN =
if(
'Sales'[c_Customer1FirstName] <> BLANK() && 'Sales'[c_Customer1LastName] <> BLANK(), 'Sales'[c_Customer1FirstName] || 'Sales'[c_Customer1LastName] & 'Sales'[v_VIN],BLANK())
This is what I tried to see of there is a matching value for NameVIN in both Sales and Consumer Funnel
VAR x = VALUES(ConsumerFunnelTime[NameVIN])
RETURN
CALCULATE(
COUNTROWS(ConsumerFunnelTime),
ALL(),
TREATAS( x, 'Sales'[NameVIN])
) + 0
I also tried this, but it is giving me an error for the syntax of CALCULATE.
DarwinSales =
VAR UsersNameVIN = ConsumerFunnelTime[NameVIN]
CALCULATE(
COUNT('Sales'[NameVIN]),
'Sales'[NameVIN] = UsersNameVIN
)
WasSale =
LOOKUPVALUE('Sales'[NameVIN], 'Sales'[NameVIN], ConsumerFunnelTime[NameVIN])
DarwinSales =
DISTINCTCOUNT(ConsumerFunnelTime[WasSale])
I am new with Dax. I have a matrix table in Power BI which has been imported from Excel. I need to create a dynamic DAX measure which will give me the values if both conditions are filtered.
This is a very big table and the measure has to be dynamic as values keep changing.
For eg:
Criteria : A, B, C
Code : 101,102,103
Values: 94, 50, 63
If Criteria is A and Code is 101 then value = 94
If Criteria is B and Code is 102 then value = 50
If you need measure based on columns that you put in your matrix on rows or columns, you may use this.
YourMeasure = IF( MAX( Tab[fruit] ) = "Apple" & MAX( Tab[size] ) = "Big", 1, 0 )
Probably what you are after, and what is safer, is adding new calculated column in your source table:
CalculatedColumn =
IF(
Tab[friut] = "Apple" // condition 1
& // AND opperator
Tab[size] = "Big", // condition 2
1, // result if true
0 // result if false
)
Where 1 is returned if both conditions are met.
If your condition path is complex you might use SWITCH function.
SWITCH( TRUE(),
Tab[column1] = "Apple" & Tab[column2]="Big", "Big Fruit",
Tab[column1] = "Carot" & Tab[column2]="Small", "Small Vegetable",
"Other thing"
)
can someone please check if my theoretical understanding of variables is correct?
Suppose that I have a table with 2 columns, Condition1 and Condition2.
I want to count the rows for which Condition1 = 4, and Condition2 = Black.
Soppose then that I write a measure called Black, that creates a Table where all rows have Condition2 = "Black". For Example:
Black:= FILTER (Table, Condition2 = "Black")
And then I write the combined code using variables:
Black4_version1 =
var B = [Black]
return = CALCULATE(
COUNTROWS(B),
Condition1 = 4)
Then this code will not work because DAX thinks that the variable B is a single number (because it's calling a measure and measure is by default seen as a single value?), even though I have created a measure that should have created a table.
But if I create the table within a variable itself, then DAX will know that it's a table and then it will work?
Black4_Version2 =
var B = FILTER (Table, Condition2 = "Black")
return = CALCULATE(
COUNTROWS(B),
Condition1 = 4)
I'm asking this because I want to be 100% sure that I have understood the answer given here: DAX: please explain why this measure with a variable will not work
also because I have been using variables already at work, so I will need to re-check all the dashboards that I have built and talk to my manager about screwing up a big time. So you could say that my job depends on this.
Variables are to be considered constants even when they contain a table. In your Black4_Version2 measure, the CALCULATE() doesn't change the COUNTROWS(B) result, since it's counting the rows of a constant table and no filtering is happening.
Black4_Version2 =
VAR B =
FILTER(
Table,
Condition2 = "Black"
)
RETURN
CALCULATE(
COUNTROWS( B ),
Condition1 = 4
)
but you can iterate over a constant table, therefore FILTER works
Black4_Version3 =
VAR B =
FILTER(
Table,
Condition2 = "Black"
)
RETURN
COUNTROWS(
FILTER(
B,
Condition1 = 4
)
)
P.S. I used the pseudo-DAX sintax used in the answer, since instead of Condition1 = 4 a column reference like Table[Column1] = 4 should be used
I would like to create a DAX formula with a IF statement.
my logic would be :
IF(column[1]= "sales" && column[2] ="chicago"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="sanfranciso"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="newyork"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="hoston"; SUM('Table'[SalesAmount]);
So, i need to calculate sales by city. how can we write above logic in dax expression in power bi?
You can use the CALCULATE function with your conditions.
For example, let's use it to calculate the sales amount of chicago
chicago_sales_amount = CALCULATE(SUM('Table'[SalesAmount]);column[1]= "sales" && (column[2] = "chicago" || column[2] = "sanfranciso" || column[2] = "newyork" || column[2] = "hoston"))
This above expression will calculate the sum of sales, only for the rows that respect the given condition.
Now you can apply the same logic for the other condition's.
CALCULATE documentation
If you want to get the sum by city but only want it when column[1] = "sales" you can summarize based on a filter:
SumByCity =
VAR curCity = 'Table'[column[2]]
RETURN
CALCULATE(SUM('Table'[SalesAmount]), FILTER(curCity = 'Table'[column[2]] && 'Table'[column[1]]= "sales"))
I am trying to make a new column that explains whether or not the particular customer no. has been handled by the employee before.
So I have the following table: Customer_ID, BookingDate, Profile_ID, Returning Customer (Yes/NO), order status. I want to add RC (Returning customer) for employee.
So my conditions for the column is that: They have been customers before, so Returning customer: Yes. They have been served by this employee before (Profile_ID) and the order status must be active, before it counts as a returning customer.
I have tried the following DAX code, but something about is wrong, because I can see it produces wrong results in the column - I believe there must be an easier way to do this, since I already have made the "Returning customer" column.
ReturningCustomerforT =
VAR mycount =
CALCULATE(
COUNTROWS(orders),
FILTER(
ALLEXCEPT(orders, orders[Column1.Customer_ID]),
orders[Column1.Profile_ID] < EARLIER(orders[Column1.Profile_ID]
) ) )
RETURN IF (mycount > 0 && orders[Column1.OrderStatus] = "Active", "Yes", "No")
For the "Returning Customer",you will need a more simple filter than "RC for employee". I will only show you the "RC for employee" as the other you can figure out from it and I also believe there you have a problem.
As you are only at "Active" orders, we need to filter them out you did this in the if clause but this is more effective in the filter.
RC for employee =
VAR custID = orders[Column1.Customer_ID]
VAR profID = orders[Column1.Profile_ID]
VAR orderDate = orders[BookingDate]
VAR orderCount = CALCULATE(COUNTROWS(orders), FILTER(orders, custID = orders[Column1.Customer_ID] && profID = orders[Column1.Profile_ID] && orderDate >= orders[BookingDate] && orders[Column1.OrderStatus] = "Active"))
RETURN IF (orderCount > 1, "Yes", "No")
So for each row of the table we will match orderid, profileid and check if orders are active, we also compare on date so all earlier bookings are included in the count.