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
newbie9292
Helper II
Helper II

Help with DAX query

Hi,

I am new to power bi and DAX world. I am trying to get data using a DAX query : 

Last_year_Kum_AE_Combined = CALCULATE(SUM(AE_Combined[AuftrEing]),FILTER(ALLSELECTED(Date_dim),Date_dim[Date]<=MAX(Date_dim[Date])))
 
Which gets the cummulative data. I want to filter this query to get only the cummulative data for the year 2020. 
 
1 ACCEPTED SOLUTION

Hi @newbie9292

 

You can create two measures as:

Running Total = 
CALCULATE (
    SUM('Table'[Sales]),
    FILTER (
        ALLSELECTED('Table'[Date]),
        'Table'[Date] <= MAX ('Table'[Date] ) && YEAR('Table'[Date])=YEAR(MAX('Table'[Date])) 
    )
)
Ly Running Total = 
CALCULATE (
    SUM('Table'[Sales]),
    FILTER (
        ALLSELECTED('Table'[Date]),
        'Table'[Date] <= MAX ('Table'[Date] ) && YEAR('Table'[Date])=YEAR(MAX('Table'[Date])) && DATEADD('Table'[Date],-1,YEAR)
    )
)

 

If you still have some question, please don't hesitate to let me known.‌‌

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

View solution in original post

10 REPLIES 10
newbie9292
Helper II
Helper II

 

@FrankAT Thank you for your solution. Although I was able to get the cummulative data for the year 2021 with your DAX query, I am struggling to get the data for the year 2020 which is in the same table. 

newbie9292_3-1626415755234.png

I am trying to get a visual like the above. The yellow line represents 2020(last year) data.

 

But when I select both year 2020 and 2021 in the slicer, this is what I get : 

 

newbie9292_4-1626415884594.png

 

Hi @newbie9292

 

You can create two measures as:

Running Total = 
CALCULATE (
    SUM('Table'[Sales]),
    FILTER (
        ALLSELECTED('Table'[Date]),
        'Table'[Date] <= MAX ('Table'[Date] ) && YEAR('Table'[Date])=YEAR(MAX('Table'[Date])) 
    )
)
Ly Running Total = 
CALCULATE (
    SUM('Table'[Sales]),
    FILTER (
        ALLSELECTED('Table'[Date]),
        'Table'[Date] <= MAX ('Table'[Date] ) && YEAR('Table'[Date])=YEAR(MAX('Table'[Date])) && DATEADD('Table'[Date],-1,YEAR)
    )
)

 

If you still have some question, please don't hesitate to let me known.‌‌

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

Hello Link,

Thanks for your reply. I tried your suggested solution but for some reason I am getting the same values for 2020 and 2021.

newbie9292_0-1626776594789.png

 

Hi @newbie9292

 

Is your issue solved?

If the issue has been solved, please adopt the solution to help others.

If you still have some question, please don't hesitate to let me known.‌‌

😉

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

Hi @newbie9292

 

You just need to select 2020 in the slicer.

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

Hello Link,

 

The problem is I want to visualize both 2020 and 2021 data on the same visual, 

newbie9292_0-1626955400146.png

 

The yellow line should be the Last year 2020 data and the dark bar should represent the 2021 data like the way it is shown.

I have 2020 and 2021 data in the same data table.

Hi @newbie9292,

 

Is your issue solved? If the issue has been solved, please adopt the solution to help others.

If you still have some question, please don't hesitate to let me known.‌‌

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

 

Hi @newbie9292,

 

In the measure I mentioned above, when 2021 is selected, Running Total returns data from 2021 and Ly Running Total returns data from 2020.

Please check.

If you still have some question, please don't hesitate to let me known.‌‌

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

FrankAT
Community Champion
Community Champion

Hi @newbie9292 ,

the following example use the year 2017 in the measure. Adjust it to year 2020:

 

09-07-_2021_00-38-27.png

 

 

Running Total of 2017 = 
CALCULATE (
    [Total Sales],
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[CalendarYear] = MAX ( 'Calendar'[CalendarYear] )
            && 'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
            && 'Calendar'[CalendarYear] = 2017
    )
)

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

 

 

AllisonKennedy
Super User
Super User

@newbie9292 

 

what is the end goal? You can filter in Power Query, report filters, slicer and DAX, but I'm not convinced that DAX is the best place for your filter without understanding more about your requirements?


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

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.