Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
Hi
I want to create a measure that calculates based on a condition: If Table1[Column1] = "KPI1" then Formula1 else Formula2.
The logic is very simple, but I can't get Power BI to read Table[Column] in my IF statement, it only allows me to use other measures from Table1.
Formula1 = DIVIDE(Sum(Num), Sum(Den), 0)
Formula2 = DIVIDE(Sum(Num), Average(Den), 0)
Does anyone know how to work around that?
Solved! Go to Solution.
@Anonymous Try:
Measure =
IF(
MAX('Table1'[Column]="KPI1",
DIVIDE(Sum(Num), Sum(Den), 0),
DIVIDE(Sum(Num), Average(Den), 0)
)
Try this @Anonymous
MeasureName =
VAR varValue =
MAX( TableName[Field] )
RETURN
IF(
varValue = "KPI1",
DIVIDE(
SUM( Table[Num] ),
SUM( Table[Den] ),
0
),
DIVIDE(
SUM( Table[Num] ),
AVERAGE( Table[Den] ),
0
)
)
You need to convert the field to a scalar value, which MAX does when used like this. MIN will give you the same result.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingTry this @Anonymous
MeasureName =
VAR varValue =
MAX( TableName[Field] )
RETURN
IF(
varValue = "KPI1",
DIVIDE(
SUM( Table[Num] ),
SUM( Table[Den] ),
0
),
DIVIDE(
SUM( Table[Num] ),
AVERAGE( Table[Den] ),
0
)
)
You need to convert the field to a scalar value, which MAX does when used like this. MIN will give you the same result.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingWorks very well!!
Still intrigued why we need to work around it like that, doesn't seem like a efficient solution... PowerBI has it's problems, it shouldn't require a scalar.
Anyway, thanks so much!
Everything in DAX requires it to either be scalar, column, or table,. and are not interchangable.
You cannot say IF(Table[Column] = "X", 1, 0) because in a measure it doesn't know how to process that column. It needs to be converted into a scalar value. SUM, MAX, AVERAGE, whatever.
In a Calculated Column you can do that because those have Row Context. Measures do not. It is a very hard concept to learn right off the bat, but once you do it becomes clear. Row Context and Filter Context are two completely different animals. Measures understand Filter Context, not Row Context.
There is another concept called Context Transition, and that is for another post. 😂
Glad you have a solution!
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI Reporting@Anonymous Try:
Measure =
IF(
MAX('Table1'[Column]="KPI1",
DIVIDE(Sum(Num), Sum(Den), 0),
DIVIDE(Sum(Num), Average(Den), 0)
)
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 9 | |
| 6 | |
| 3 | |
| 2 | |
| 1 |
| User | Count |
|---|---|
| 21 | |
| 14 | |
| 9 | |
| 5 | |
| 5 |