Forum Discussion

harshad_barge's avatar
6 years ago

Modifying DAX query to extract correct value

Hi all, 
So I am essentially trying to have a column that is grouped by sum of payment. I want just the cumulative Pay when the payroll date is greater than or equal to the Calculation Date. The grouped values are correct but are repeating hence, when I show it in a table, and sum it, it gives an incorrect value.

The DAX for calculated Pay (N) used is:

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 to desired effect is: 
=IF(AND(E12>=D12,F12<>F13),F12,0)
 
The problem I am facing is that I cannot use it (owing to my limited capabilities; I am quite new to DAX) in DAX as there seems no way to reference previous row

The table and the expected output is as below:


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

 

How can I modify my Dax to ensure that the output is as per Expected (Excel Formula) column.

p.s. The column that is incorrectly (for intents and purposes) pulling the "Calculated Pay" column is:

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))    

                )
There are a few instances when the calculation date is being skipped at the applicable payroll date hence, the CalculatedPay sum is being skipped and the overall aggregation is thus incorrect.

Greg_Deckler Grateful for your help so far. I am just not getting there with what I want. I hope this post helps better.


Thank you all!

5 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    It seems like I have seen this problem before, is there another similar thread that you posted? If so can you post a link to it so that I can reference it and what I did? I'll take a look at this but there are a lot of dates I need to correct because they don't work with my US English Power BI Desktop. I need to create a Power Query function to convert dates. So annoying!! 🙂

  • Anonymous's avatar
    Anonymous
    Not applicable
    If you are trying to add a calculated column to a table in the model, then please spare yourself pain and grief - USE POWER QUERY.

    Best
    D
    • harshad_barge's avatar
      harshad_barge
      Helper I

      Is there any way I can use a DAX calculated table in power query?
      If that would be the case, things would be much easier.