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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
haripbi
New Member

Get last 2 years data from measure based on slicer

Hi All,

Need some help on the below dax statement.

Requirement:

Slicer = year (example 2023)
Table = Fact_Availability

Need count of equipmentno with filter as HireAvailability = "On Hire" for the last 2 years based on year from slicer.

Tried below but no luck.

Measure =
VAR StartDate = FIRSTDATE(Fact_Availability[CalendarDate])
VAR StartDateLY = DATEADD(StartDate,-2,YEAR)
VAR EndDate = LASTDATE(Fact_Availability[CalendarDate])
RETURN
CALCULATE(
COUNT('Fact_Availability'[EquipmentNo]),
'Fact_Availability'[HireAvailability]="On Hire",
'Fact_Availability'[CalendarDate] >= StartDateLY && 'Fact_Availability'[CalendarDate] <= EndDate

 

The above measure returns only 2023 data because the year slicer has 2023 selected.


When i check variable values individually it shows "StartDateLY" = 2021 and EndDate = 2023 which is correct.

but the above measure is rendering only 2023 data as the filter is 2023.

I need a dax statement which takes input from slicer as 2023 and gets the count of equipment numbers from 2021 till 2023.

1 ACCEPTED SOLUTION
v-yilong-msft
Community Support
Community Support

Hi @haripbi ,

@lbendlin , thanks for your concern about this case. I tried to create a sample data myself based on the user's requirement and implemented the result. Please check if there is anything that can be improved. Here is my solution:  

I create a table as you mentioned.

vyilongmsft_0-1713852368994.png

Then I create a new table. It is used as slicer.

Table = VALUES('Fact_Availability'[Year])

vyilongmsft_1-1713852656282.png

Then I create a measure and you will get what you want.

MEASURE =
VAR _Slicer =
    MAX ( 'Table'[Year] )
RETURN
    IF (
        YEAR ( MAX ( 'Fact_Availability'[CalendarDate] ) ) >= _Slicer - 2
            && YEAR ( MAX ( 'Fact_Availability'[CalendarDate] ) ) <= _Slicer,
        1
    )

vyilongmsft_2-1713853004848.pngvyilongmsft_3-1713853015980.png

 

 

 

Best Regards

Yilong Zhou

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yilong-msft
Community Support
Community Support

Hi @haripbi ,

@lbendlin , thanks for your concern about this case. I tried to create a sample data myself based on the user's requirement and implemented the result. Please check if there is anything that can be improved. Here is my solution:  

I create a table as you mentioned.

vyilongmsft_0-1713852368994.png

Then I create a new table. It is used as slicer.

Table = VALUES('Fact_Availability'[Year])

vyilongmsft_1-1713852656282.png

Then I create a measure and you will get what you want.

MEASURE =
VAR _Slicer =
    MAX ( 'Table'[Year] )
RETURN
    IF (
        YEAR ( MAX ( 'Fact_Availability'[CalendarDate] ) ) >= _Slicer - 2
            && YEAR ( MAX ( 'Fact_Availability'[CalendarDate] ) ) <= _Slicer,
        1
    )

vyilongmsft_2-1713853004848.pngvyilongmsft_3-1713853015980.png

 

 

 

Best Regards

Yilong Zhou

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

lbendlin
Super User
Super User

Your slicer needs to be fed from a disconnected table. Then you use measures to sense its value and compute the expected outcome.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors