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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
harshad_barge
Helper I
Helper I

Help Please Incorrect DAX code

Hi all,

I am trying to edit an incorrect DAX code which is pulling out values as per this table: 
The expected values as as per the Expected Excel column.

 

Employee NameAUD Total Daily EarningsCalculatedPayCalculation DateApplicable Payroll DateCalculated Pay (N)Expected (Excel Formula)
Arsher B$327.81$2,031.341/09/2013 0:001/09/2013 0:002031.342031.34
Arsher B$178.60$0.004/09/2013 0:0015/09/2013 0:001959.180
Arsher B$185.73$0.005/09/2013 0:0015/09/2013 0:001959.180
Arsher B$162.02$0.006/09/2013 0:0015/09/2013 0:001959.180
Arsher B$228.94$0.007/09/2013 0:0015/09/2013 0:001959.180
Arsher B$292.16$0.008/09/2013 0:0015/09/2013 0:001959.180
Arsher B$151.95$0.0011/09/2013 0:0015/09/2013 0:001959.180
Arsher B$151.96$0.0012/09/2013 0:0015/09/2013 0:001959.180
Arsher B$151.96$0.0013/09/2013 0:0015/09/2013 0:001959.180
Arsher B$189.94$0.0014/09/2013 0:0015/09/2013 0:001959.180
Arsher B$265.92$1,959.1815/09/2013 0:0015/09/2013 0:001959.181959.18
Arsher B$141.82$0.0018/09/2013 0:0029/09/2013 0:002063.780
Arsher B$166.15$0.0019/09/2013 0:0029/09/2013 0:002063.780
Arsher B$271.50$0.0020/09/2013 0:0029/09/2013 0:002063.780
Arsher B$192.47$0.0021/09/2013 0:0029/09/2013 0:002063.780
Arsher B$269.46$0.0022/09/2013 0:0029/09/2013 0:002063.780
Arsher B$151.95$0.0025/09/2013 0:0029/09/2013 0:002063.780
Arsher B$151.96$0.0026/09/2013 0:0029/09/2013 0:002063.780
Arsher B$187.24$0.0027/09/2013 0:0029/09/2013 0:002063.780
Arsher B$221.65$0.0028/09/2013 0:0029/09/2013 0:002063.780
Arsher B$309.58$2,063.7829/09/2013 0:0029/09/2013 0:002063.782063.78
Daniel Gelat$151.96$0.0030/09/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$171.93$0.001/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$151.95$0.002/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$289.32$0.005/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$333.89$0.006/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$405.20$0.007/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$151.96$0.008/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$141.82$0.009/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$187.85$0.0010/10/2013 0:0013/10/2013 0:002167.230
Daniel Gelat$181.35$0.0011/10/2013 0:0013/10/2013 0:002167.232167.23
Daniel Gelat$169.19$0.0014/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$184.08$0.0015/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$173.75$0.0018/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$247.18$0.0019/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$285.67$0.0020/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$151.96$0.0021/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$151.96$0.0022/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$151.96$0.0023/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$254.88$0.0026/10/2013 0:0027/10/2013 0:002036.550
Daniel Gelat$265.92$2,036.5527/10/2013 0:0027/10/2013 0:002036.552036.55
Daniel Gelat$202.35$0.0030/10/2013 0:0010/11/2013 0:001928.30
Daniel Gelat$190.45$0.0031/10/2013 0:0010/11/2013 0:001928.31928.3

 

The DAX that calculates the CalculatedPay column is as  below. However, since some times the Calculation Date is skipped (not equal) to the Applicable Payroll Date, it outputs 0 which is not desired.

 

CalculatedPay = IF
                (
                    [Calculation Date] < [Applicable Payroll Date],
                    --then--
                    0,
                    --else--
                    VAR payrollId = [Payroll Day ID]
                    VAR empID = [Employee_ID]
                    RETURN CALCULATE(SUM('Ordinary Hours'[AUD Total Daily Earnings]),FILTER('Ordinary Hours',[Payroll Day ID]=payrollId && [Employee_ID]=empID))    
                )

 I then tried to do a group by sum  as below however, it repeats the column values and when I display in the viz, the aggregate sum of that column is incorrect

 

Calculated Pay (N) = 
CALCULATE (
SUM ( 'Ordinary Hours'[AUD Total Daily Earnings] ),
ALLEXCEPT (
'Ordinary Hours',
'Ordinary Hours'[Employee Name],
'Ordinary Hours'[Applicable Payroll Date].[Date]
)
)

 

The excel formula that works is:

 

=IF(AND(E2>=D2,F2<>F3),F2,0)

 

Of course excel can refer to cells in above and below rows but this is the DAX part (I am new to this) I am trying to figure out.

@Greg_Deckler suggested me to go down the index path here however, I am to new with Power BI to do it correctly.
Thank you so much!
Please help me with the correct code for the CalculatedPay or Calculated Pay (N) column.

Many Many Thanks!

 

4 REPLIES 4
Anonymous
Not applicable

I can't understand why you people always want to write complex DAX, completely unmaintainable and hardly comprehensible when things are so dead simple in Power Query... Mate, do yourself a favor and please do this calculation in POwer Query. You'll thank me later.

Best
D

I would if I could. This is a calculated table. I am trying to write DAX on a calculated table. Hence cannot do this in Power query.
Anonymous
Not applicable

Calculate the table in PQ as well.

Best
D
az38
Community Champion
Community Champion

@harshad_barge 

try like a measure

Measure = 
var _nextRow = CALCLATE(min('Ordinary Hours'[Calculation Date]), ALLEXCEPT('Ordinary Hours', 'Ordinary Hours'[Employee Name], 'Ordinary Hours'[Payroll Day ID]) )

RETURN
IF (SELECTEDVALUE('Ordinary Hours'[Applicable Payroll Date]) >= SELECTEDVALUE('Ordinary Hours'[Calculation Date]), 
0,
CALCULATE(SUM('Ordinary Hours'[AUD Total Daily Earnings]), ALLEXCEPT('Ordinary Hours', 'Ordinary Hours'[Employee Name], 'Ordinary Hours'[Payroll Day ID]), 'Ordinary Hours'[Calculation Date]=_nextRow) 
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.