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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
BraunReivn
Regular Visitor

DAX Formula

Good day!

Please pardon me, I'm new in this community and a complete newbie in PBI. I'll try to be clear and detailed as possible.

So, here's my problem.

I have 2 tables - Table 1 and Table 2.

Table 1

CodeDateStageHCCode_Stage
(calculated column)
Stage_HC
(calculated column)
X456701/14/2023C14X4567C

14

X456712/10/2022B15X4567B15
X456712/05/2022B17X4567B15
X456710/22/2022A20X4567A20

Sample table; there's a long list of data.

 

I need to get the latest HC per Code and Stage.

In the table above, X4567B has 2 entries -- 15 HC on 12/10/2022 and 17 HC on 12/05/2022.

In this case, X4567B would take 15 as its HC.

 

To get the latest HC, I created a calculated column - Stage_HC

Stage_HC =

CALCULATE(

     SUM('Table 1'[HC]),

     FILTER('Table 1','Table 1'[Date]

     =CALCULATE(

     MAX('Table 1'[Date]),

     FILTER('Table 1','Table 1'[Code_Stage]=EARLIER('Table 1'[Code_Stage])))

     && 'Table 1'[Code_Stage]=EARLIER('Table 1'[Code_Stage])))

 

Table 2 (related to Table 1 by Code_Stage)

Respondent IDDateCodeStageCode_Stage
(calculated column)
11417800011/17/2022x4567Ax4567A
11417800111/17/2022x4567Ax4567A
11417800211/17/2022x4567Ax4567A
11417800311/17/2022x4567Ax4567A
11417800411/17/2022x4567Ax4567A
11417800512/10/2022x4567Bx4567B
11417800612/10/2022x4567Bx4567B
11417800712/10/2022x4567Bx4567B
11417800812/10/2022x4567Bx4567B
11417800912/10/2022x4567Bx4567B
11417801012/25/2022x4567Bx4567B
11417801112/25/2022x4567Bx4567B
11417801212/25/2022x4567Bx4567B
11417801312/25/2022x4567Bx4567B
11417801412/25/2022x4567Bx4567B
11417801512/25/2022x4567Bx4567B
11417801612/25/2022x4567Bx4567B
11417801712/25/2022x4567Bx4567B
11417801812/25/2022x4567Bx4567B
11417801912/25/2022x4567Bx4567B

 

The desired output should be like this.

Table Visual

Respondent IDDateCode_StageStage# of Respondent
(calculated measure)
Stage_HCTake Rate
(calculated measure)
11417800011/17/2022x4567AA52025%
11417800111/17/2022x4567AA52025%
11417800211/17/2022x4567AA52025%
11417800311/17/2022x4567AA52025%
11417800411/17/2022x4567AA52025%
11417800512/10/2022x4567BB51533%
11417800612/10/2022x4567BB51533%
11417800712/10/2022x4567BB51533%
11417800812/10/2022x4567BB51533%
11417800912/10/2022x4567BB51533%
11417801012/25/2022x4567BB101567%
11417801112/25/2022x4567BB101567%
11417801212/25/2022x4567BB101567%
11417801312/25/2022x4567BB101567%
11417801412/25/2022x4567BB101567%
11417801512/25/2022x4567BB101567%
11417801612/25/2022x4567BB101567%
11417801712/25/2022x4567BB101567%
11417801812/25/2022x4567BB101567%
11417801912/25/2022x4567BB101567%

 

Card Visual (Overall take rate)

BraunReivn_0-1672988377481.png

where:

# of Respondent = count of respondents based on date, code, stage

Take Rate = # of Respondent divide by Stage_HC

 

Calculated measure:

# of Respondent =

CALCULATE(

     COUNTROWS('Table 2'),

     FILTER('Table 2','Table 2'[Stage]='Table 2'[Stage] &&

     'Table 2'[Date]='Table 2'[Date]))

 

Take Rate =

DIVIDE(COUNT('Table 2'[# of Respondent]),SELECTEDVALUE('Table 1',[Stage_HC]))

 

These measures managed to get me the desired Table visual output but not the Card visual which returns (Blank).

 

I hope I was able to explain it clearly, and not violating any forum rules. 

Thanks in advance.

 

BraunReivn

 

 

 

 

 

 

 

 

 

2 ACCEPTED SOLUTIONS
v-zhangti
Community Support
Community Support

Hi, @BraunReivn 

 

You can try the following methods.
Measure:

# of Respondent = CALCULATE(COUNT('Table 2'[Respondent ID]),ALLEXCEPT('Table 2','Table 2'[Date],'Table 2'[Code_Stage]))
Satge_HC M = CALCULATE(MAX('Table 1'[Stage_HC]),ALLEXCEPT('Table 2','Table 2'[Code_Stage]))
Take Rate = 
Var _N1=SUMX('Table 2',[# of Respondent])
Var _N2=SUMX('Table 2',[Satge_HC M])
Return
DIVIDE(_N1,_N2)

vzhangti_0-1673243491859.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

If you change the grouping in your visual you also need to change the formula in the measure.

 

 

Take Rate = 
var a = SUMMARIZE('Table 2','Table 2'[Date],'Table 2'[Stage],'Table 2'[Code],"tr",DIVIDE([# of Respondent],[Max HC],0))
return AVERAGEX(a,[tr])

see attached.

 

 

View solution in original post

11 REPLIES 11
v-zhangti
Community Support
Community Support

Hi, @BraunReivn 

 

You can try the following methods.
Measure:

# of Respondent = CALCULATE(COUNT('Table 2'[Respondent ID]),ALLEXCEPT('Table 2','Table 2'[Date],'Table 2'[Code_Stage]))
Satge_HC M = CALCULATE(MAX('Table 1'[Stage_HC]),ALLEXCEPT('Table 2','Table 2'[Code_Stage]))
Take Rate = 
Var _N1=SUMX('Table 2',[# of Respondent])
Var _N2=SUMX('Table 2',[Satge_HC M])
Return
DIVIDE(_N1,_N2)

vzhangti_0-1673243491859.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

Thank you so much for noticing and posting a reply. I'm sure this one will work as well.

lbendlin
Super User
Super User

1. Please note that Power Query is case sensitive but Power BI is not.  Your Code column values are inconsistent

2.  For the latest HC per Code and Stage you can use REMOVEFILTERS or ALL. You will need to decide if this should be a measure (can be influenced by filters)  or a calculated column (immutable).  Are you planning to allow your users to filter by date ranges?

 

A calculated column could look like this:

Latest HC = 
var st = [Stage]
var cd = [Code]
var md = CALCULATE(max([Date]),All('Table 1'),'Table 1'[Code]=cd,'Table 1'[Stage]=st)
return CALCULATE(max([HC]),All('Table 1'),'Table 1'[Date]=md,'Table 1'[Code]=cd,'Table 1'[Stage]=st)

3. You could create composite keys Stage_Code  but you can also use TREATAS to transfer filters from one table to another.

Max HC = CALCULATE(max('Table 1'[Latest HC]),TREATAS(SUMMARIZE('Table 2','Table 2'[Stage],'Table 2'[Code]),'Table 1'[Stage],'Table 1'[Code]))

4. Your take rate computation is not clear - here is a standard approach to average the value across respondents so that the result can be used both for the rows and for the totals:

Take Rate = 
var a = summarize('Table 2','Table 2'[Respondent ID],"tr",divide([# resp],[Max HC],0))
return AVERAGEX(a,[tr])

 see attached.

Thank you @lbendlin  for noticing my post and taking the time to answer.

So I followed everything however it's not giving me the correct overall take rate.  I wonder what's missing here.

I'd like so much to attach my file however I can't seem to find the option to do it so I'm pasting a screenshot instead.

Hoping once again for your help.

 

Screenshot 2023-01-11 033846.png

Change the implicit measure for the Take Rate column to Average rather than Sum.

 

You will need to decide if this should be a measure (can be influenced by filters)  or a calculated column (immutable).  Are you planning to allow your users to filter by date ranges?

Thank you once again @lbendlin.

 

To answer your question, yes the users may want to filter by date ranges.

 

As for changing the Take Rate to Average, is there a way to do it in a measure? Take Rate is a measure, not a calculated column.

 

My apologies if this is taking longer than it should. Your help is greatly appreciated.

If you look at my proposal you see that I am using AVERAGEX.  Can you confirm that you use the same?

Yes, I did. 

would you be able to post the sample pbix somewhere?

If you change the grouping in your visual you also need to change the formula in the measure.

 

 

Take Rate = 
var a = SUMMARIZE('Table 2','Table 2'[Date],'Table 2'[Stage],'Table 2'[Code],"tr",DIVIDE([# of Respondent],[Max HC],0))
return AVERAGEX(a,[tr])

see attached.

 

 

Thank you so much for all your help.  You're amazing!😊

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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.

Top Kudoed Authors