Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
toum
Helper II
Helper II

Wrong DAX

Please, I need your help. This DAX formula does not work. Could you please tell me what I am doing wrong?

I got True/false error. But when I get rid of the variable AmountCateg and I do the calculation of the Switch inside the big measure it works fine, but I want to use the AmountCateg as a variable because I will do another switch for the end result.

 

VAR SelectedColumn = SELECTEDVALUE('Categories'[ID])
VAR AmountCateg =
SWITCH(
SelectedColumn,
"A", Production[AmountB],
"B", Production[AmountB],
"C", Production[AmountC],
0 -- Default value in case SelectedColumn is not "A", "B", or "C"
)

RETURN
CALCULATE(
COUNTROWS(Production),
AmountCateg >= 1000
)

 

 

Thank you. 

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

@toum Maybe:

VAR SelectedColumn = SELECTEDVALUE('Categories'[ID])
VAR AmountCateg =
SWITCH(
SelectedColumn,
"A", Production[AmountB],
"B", Production[AmountB],
"C", Production[AmountC],
0 -- Default value in case SelectedColumn is not "A", "B", or "C"
)

RETURN
CALCULATE(
COUNTROWS(Production),
[AmountCateg] >= 1000
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

v-linyulu-msft
Community Support
Community Support

Thanks for the reply from @Greg_Deckler , please allow me to provide another insight:

HI,@toum 

Regarding the issue you raised, my solution is as follows:

1.First I have created the following table and the column names and data are the data you have given:

vlinyulumsft_0-1718600437518.png

vlinyulumsft_1-1718600453261.png

2.I then created the following two measure:

AmountB = CALCULATE(SUM('Production'[VALUES]),'Production'[ID]="B")
AmountC = CALCULATE(SUM('Production'[VALUES]),'Production'[ID]="C")

3.Then I will you provide the code as a measure value, he also reported an error:

vlinyulumsft_2-1718600531456.png

The main reason for this error is that the filter criteria you provide are not clear.

4.Here's assuming your need is to determine if the current metric is greater than 1000, and then count the number of rows with the same id as the slicer:

Measure = 
VAR SelectedColumn = SELECTEDVALUE('Categories'[ID])
VAR AmountCateg =
SWITCH(
SelectedColumn,
"A", Production[AmountB],
"B", Production[AmountB],
"C", Production[AmountC],
0 -- Default value in case SelectedColumn is not "A", "B", or "C"
)

RETURN
IF(AmountCateg>1000,CALCULATE(COUNTROWS('Production'),'Production'[ID]=SelectedColumn),0)

5.Here's my final result, which I hope meets your requirements.

vlinyulumsft_3-1718600569186.png

vlinyulumsft_4-1718600577334.png

Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.


Please find the attached pbix relevant to the case.

 

Best Regards,

Leroy Lu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

View solution in original post

2 REPLIES 2
v-linyulu-msft
Community Support
Community Support

Thanks for the reply from @Greg_Deckler , please allow me to provide another insight:

HI,@toum 

Regarding the issue you raised, my solution is as follows:

1.First I have created the following table and the column names and data are the data you have given:

vlinyulumsft_0-1718600437518.png

vlinyulumsft_1-1718600453261.png

2.I then created the following two measure:

AmountB = CALCULATE(SUM('Production'[VALUES]),'Production'[ID]="B")
AmountC = CALCULATE(SUM('Production'[VALUES]),'Production'[ID]="C")

3.Then I will you provide the code as a measure value, he also reported an error:

vlinyulumsft_2-1718600531456.png

The main reason for this error is that the filter criteria you provide are not clear.

4.Here's assuming your need is to determine if the current metric is greater than 1000, and then count the number of rows with the same id as the slicer:

Measure = 
VAR SelectedColumn = SELECTEDVALUE('Categories'[ID])
VAR AmountCateg =
SWITCH(
SelectedColumn,
"A", Production[AmountB],
"B", Production[AmountB],
"C", Production[AmountC],
0 -- Default value in case SelectedColumn is not "A", "B", or "C"
)

RETURN
IF(AmountCateg>1000,CALCULATE(COUNTROWS('Production'),'Production'[ID]=SelectedColumn),0)

5.Here's my final result, which I hope meets your requirements.

vlinyulumsft_3-1718600569186.png

vlinyulumsft_4-1718600577334.png

Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.


Please find the attached pbix relevant to the case.

 

Best Regards,

Leroy Lu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

Greg_Deckler
Super User
Super User

@toum Maybe:

VAR SelectedColumn = SELECTEDVALUE('Categories'[ID])
VAR AmountCateg =
SWITCH(
SelectedColumn,
"A", Production[AmountB],
"B", Production[AmountB],
"C", Production[AmountC],
0 -- Default value in case SelectedColumn is not "A", "B", or "C"
)

RETURN
CALCULATE(
COUNTROWS(Production),
[AmountCateg] >= 1000
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.