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
apatwal
Helper III
Helper III

Help in DAX Building

Hi,

 

I have below requirement to create DAX.

I need to take difference of margin for 2022 year and 2021 year on weekly basis and plot this in column chart that should show only 2022 year data month wise in normal as well as in a cumulative way.

 

For example : For week of Jan 3, 2022, we need to calculate the week’s total margin and then subtract the margin from week of Jan 4, 2021 and plot this difference month wise on column chart and after that need to take cumulative of these values also.

 

For sample records, please find attached PBI file.

PBI Sample File

 

Thanks..

 

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @apatwal 

 

You can add a Year column to your table

Year = YEAR('Sample Data'[Week Start Date])

 

Then create the following measures:

Margin This Year = SUM('Sample Data'[Margin $])
Margin Previous Year = 
VAR _thisYear = MAX('Sample Data'[Year])
VAR _thisWeek = MAX('Sample Data'[Week No])
RETURN
CALCULATE(SUM('Sample Data'[Margin $]),ALL('Sample Data'),'Sample Data'[Year]=_thisYear-1,'Sample Data'[Week No]=_thisWeek)
Margin Diff = [Margin Previous Year] - 'Sample Data'[Margin This Year]
Cumulative Diff =
SUMX (
    FILTER (
        CALCULATETABLE (
            VALUES ( 'Sample Data'[Week Start Date] ),
            ALLEXCEPT ( 'Sample Data', 'Sample Data'[Year] )
        ),
        'Sample Data'[Week Start Date] <= MAX ( 'Sample Data'[Week Start Date] )
    ),
    [Margin Diff]
)

 

Drag Week Start Date and measures into a visual. Put Year column into the visual as a filter field and set its value is 2022. 

vjingzhang_1-1646289949732.png

 

You will get below result. 

vjingzhang_0-1646289877768.png

 

* I don't make it month wise in the column chart. In the following example, the week start date of week 5 in 2022 is in January but the same day of 2021 is in February. When they are in different months, the measure may return a wrong result when the axis is month wise. 

vjingzhang_2-1646290098253.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @apatwal 

 

You can add a Year column to your table

Year = YEAR('Sample Data'[Week Start Date])

 

Then create the following measures:

Margin This Year = SUM('Sample Data'[Margin $])
Margin Previous Year = 
VAR _thisYear = MAX('Sample Data'[Year])
VAR _thisWeek = MAX('Sample Data'[Week No])
RETURN
CALCULATE(SUM('Sample Data'[Margin $]),ALL('Sample Data'),'Sample Data'[Year]=_thisYear-1,'Sample Data'[Week No]=_thisWeek)
Margin Diff = [Margin Previous Year] - 'Sample Data'[Margin This Year]
Cumulative Diff =
SUMX (
    FILTER (
        CALCULATETABLE (
            VALUES ( 'Sample Data'[Week Start Date] ),
            ALLEXCEPT ( 'Sample Data', 'Sample Data'[Year] )
        ),
        'Sample Data'[Week Start Date] <= MAX ( 'Sample Data'[Week Start Date] )
    ),
    [Margin Diff]
)

 

Drag Week Start Date and measures into a visual. Put Year column into the visual as a filter field and set its value is 2022. 

vjingzhang_1-1646289949732.png

 

You will get below result. 

vjingzhang_0-1646289877768.png

 

* I don't make it month wise in the column chart. In the following example, the week start date of week 5 in 2022 is in January but the same day of 2021 is in February. When they are in different months, the measure may return a wrong result when the axis is month wise. 

vjingzhang_2-1646290098253.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

amitchandak
Super User
Super User

@apatwal , To get week year behind measure try like example

week Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-364,DAY))

 

For normal year

Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),SAMEPERIODLASTYEAR('Date'[Date]))

 

 

This should give diff any period vs any period based in grouping 

 

Time Intelligence, DATESMTD, DATESQTD, DATESYTD, Week On Week, Week Till Date, Custom Period on Period,
Custom Period till date: https://youtu.be/aU2aKbnHuWs

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.