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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
zakkyang
Frequent Visitor

groupby multiple columns and calculate the difference base on the date

Hi,


There are columns like: week number, product, country and sales. I want to group by product and country to calculate the difference between previous week (order by week in the group). Any idea? Thank you in advance.

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @zakkyang 

If the columns you mentioned are all from the same table, you could create a measure like below to calculate the difference between each week and its previous week. And put product, country, week columns, sum of sales and the Difference measure into a matrix visual to check the differences.

Difference = 
VAR _thisWeekNumber = SELECTEDVALUE('Table (2)'[Week Number])
VAR _thisWeekSales = SUM('Table (2)'[Sales])
VAR _previousWeekSales = CALCULATE(SUM('Table (2)'[Sales]),ALLEXCEPT('Table (2)','Table (2)'[Product],'Table (2)'[Country]),'Table (2)'[Week Number]=_thisWeekNumber-1)
RETURN
IF(ISBLANK(_previousWeekSales),BLANK(),_thisWeekSales - _previousWeekSales)

042004.jpg

Download the attached .pbix for details.


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

View solution in original post

4 REPLIES 4
v-jingzhang
Community Support
Community Support

Hi @zakkyang 

If the columns you mentioned are all from the same table, you could create a measure like below to calculate the difference between each week and its previous week. And put product, country, week columns, sum of sales and the Difference measure into a matrix visual to check the differences.

Difference = 
VAR _thisWeekNumber = SELECTEDVALUE('Table (2)'[Week Number])
VAR _thisWeekSales = SUM('Table (2)'[Sales])
VAR _previousWeekSales = CALCULATE(SUM('Table (2)'[Sales]),ALLEXCEPT('Table (2)','Table (2)'[Product],'Table (2)'[Country]),'Table (2)'[Week Number]=_thisWeekNumber-1)
RETURN
IF(ISBLANK(_previousWeekSales),BLANK(),_thisWeekSales - _previousWeekSales)

042004.jpg

Download the attached .pbix for details.


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

Hi @v-jingzhang 

Thank you so much much for this. I spent much of yesterday pulling my hair out over one similar to this and couldn't find a similar question in community.

 

The closest I got was using a measure like this:

diff = 
CALCULATE (
        SUM ( 'Key Webpages'[Pageviews] ), 'Key Webpages'[Months back] 
    )
        - CALCULATE (
            SUM ( 'Key Webpages'[Pageviews] ), 'Key Webpages'[Months back] -1 
        )

I adjusted yours and it worked perfectly! Would you mind telling, is the reason you use variables here (aside from neatness/simplicity) that it enables you to use the SELECTEDVALUE function where it wouldn't otherwise work (i.e. in a T/F expression)?

zakkyang
Frequent Visitor

Hi ,

 

I got lost in your contents. Could you give a direct solution with code? I am new to PowerBI and please allow me to digest through your code directly. Thanks a lot.

 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.