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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Rai_BI
Helper IV
Helper IV

DAX measure to calculate sales, filtering by default the last 12 months

In Power BI, I need a DAX measure to calculate sales, filtering, by default, the last 12 months, however, if there is an external filter in the calendar table, the sales value to be returned must respect the external filter, if there is no external filter in the calendar table then the value to return must be the sales corresponding to the last 12 months. In the calendar table there is a column that identifies the last 12 months where the month that is part of the last 12 months is marked as 1 and the other months as 0. The name of this column is "[Last 12 Months]".

The DAX measure below partially meets these requirements, however, when performing an external filter on the calendar table that is outside the range of the last 12 months, the value returned is zero and this needs to be corrected so that if there is an external filter on the calendar table, the value returned must match the applied filter.

DAX...
IF(
NOT ISFILTERED('dCalendar'),
CALCULATE(
[USD Sales],
ALL('dCalendar'), -- Added to remove existing filters in the "dCalendar" table
'dCalendar'[Last 12 Months] = 1
),
CALCULATE(
[USD Sales],
'dCalendar',
'dCalendar'[Last 12 Months] = 1
)
)

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, I am not sure if I understood your question correctly, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

I hope the below can provide some ideas on how to create a solution for your semantic model.

Jihwan_Kim_1-1703045085469.png

 

 

Jihwan_Kim_0-1703045052440.png

 

expected result measure: =
VAR _nofilterdayscount =
    CALCULATE ( COUNTROWS ( dCalendar ), REMOVEFILTERS () )
VAR _filterdayscount =
    CALCULATE ( COUNTROWS ( dCalendar ), ALLSELECTED ( dCalendar ) )
VAR _qty =
    SUM ( Sales[Quantity] )
VAR _qtylast12months =
    CALCULATE ( SUM ( Sales[Quantity] ), dCalendar[Last 12 Months] = 1 )
RETURN
    IF ( _nofilterdayscount = _filterdayscount, _qtylast12months, _qty )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

View solution in original post

2 REPLIES 2
Rai_BI
Helper IV
Helper IV

Thank you very much!! This was exactly what i was looking for

Jihwan_Kim
Super User
Super User

Hi, I am not sure if I understood your question correctly, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

I hope the below can provide some ideas on how to create a solution for your semantic model.

Jihwan_Kim_1-1703045085469.png

 

 

Jihwan_Kim_0-1703045052440.png

 

expected result measure: =
VAR _nofilterdayscount =
    CALCULATE ( COUNTROWS ( dCalendar ), REMOVEFILTERS () )
VAR _filterdayscount =
    CALCULATE ( COUNTROWS ( dCalendar ), ALLSELECTED ( dCalendar ) )
VAR _qty =
    SUM ( Sales[Quantity] )
VAR _qtylast12months =
    CALCULATE ( SUM ( Sales[Quantity] ), dCalendar[Last 12 Months] = 1 )
RETURN
    IF ( _nofilterdayscount = _filterdayscount, _qtylast12months, _qty )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

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!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.