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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors