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
slavisha84
Helper I
Helper I

How to aggregate revenue by fiscal week?

Hello, 
I have one dataset with 2 columns: Date and Revenue. The Date column contains dates from 2020-01-06 to 2021-12-31. The second column contains Revenue data. 

slavisha84_0-1617928314847.png

 

I have another dataset in which I have a fiscal calendar table for the same data range as the first dataset. 

 

slavisha84_1-1617928502743.png

 

What I need t do is to merge these two datasets into a new table using the date as a cross-reference, and after that transform that new table to look like this:

FiscalWeek2020 Revenue2021 Revenue
1747732
2720544
3739925
4667652

 

So basically I need to separate revenue for 2020 and 2021 by fiscal week. 
Since there are multiple records for each date I am assuming I will need to do the sum.

 

What would be the easiest way to do this?

 

If you need sample data please I am uploading it to the shared folder below:

https://1drv.ms/u/s!AhhZq1add5YwjYIvuASi76lCL3R1eA?e=sZQshq

 

 

1 ACCEPTED SOLUTION

OK, you can calculate the yearly 4 week rolling average with your data as is:

jdbuchanan71_0-1617937919884.png

This is the measure for 2020:

2020 4w ra = 
VAR _Weeks = 4
VAR _FiscalWeek = SELECTEDVALUE ( FiscalCalendar[FiscalWeek] )
RETURN
    IF (
        NOT ISBLANK ( _FiscalWeek ),
        DIVIDE (
            CALCULATE (
                [Revenue Amount],
                CALCULATETABLE (
                    DATESINPERIOD (
                        FiscalCalendar[Date],
                        MAX ( FiscalCalendar[Date] ),
                        - ( _Weeks * 7 ),
                        DAY
                    ),
                    FiscalCalendar[FiscalYear] = 2020
                ),
                VALUES ( FiscalCalendar[FiscalYear] )
            ),
            _Weeks
        )
    )

But, if you are dead set on having a new table you can create it using this code.

New Table = 
ADDCOLUMNS(
    SUMMARIZE(FiscalCalendar,FiscalCalendar[FiscalWeek]),
    "2020",CALCULATE(SUM(RevenueByDate[Revenue]),FiscalCalendar[FiscalYear] = 2020),
    "2021",CALCULATE(SUM(RevenueByDate[Revenue]),FiscalCalendar[FiscalYear] = 2021)
)

jdbuchanan71_1-1617938029181.png

I have updated the attached file showing both solutions.

 

 

 

View solution in original post

6 REPLIES 6
jdbuchanan71
Super User
Super User

@slavisha84 

Thank you for the sample .pbix, it makes it much easier to help.  All you need to is join the calendar table to the revenue table by the date field.

jdbuchanan71_0-1617933112892.png

Then write a measure to sum the revenue.

Revenue Amount = SUM ( RevenueByDate[Revenue] )

And add the values from your calendar table and the measure to a visual.

jdbuchanan71_1-1617933192310.png

I have attached my updated version of your sample so you can look at it.

@jdbuchanan71 
Thank you for your quick response. So what I need is an actual real new table because I will be doing some other operation on it. The table itself should look like the visual you produced. I just need it in form of a table.

@slavisha84 

I don't think you do need a new table.  You want to be doing your operation using the date table and your revenue table as it is.  That way you can use time intelligence functions and slice the data by whatever other categorical date you have.  
Forcing your data into a new table that looks like the visual would make it less useful.  What if you want to summarize it by month or quarter instead of week?  With the model as is that is easy:

jdbuchanan71_0-1617935807284.png

Here is a good free course on data modeling for PowerBI you might find helpful.  Getting you data model right from the beginning makes is easier to maintain, easier to extend and faster.
https://www.sqlbi.com/p/introduction-to-data-modeling-for-power-bi-video-course/

 

@jdbuchanan71 The reason why I need it in the real table is that I need to perform 4 Week Rolling Average on each of these Columns and then plot it on Line Chart. So it should be One Chart that will give me 3 lines. 3rd line would be Forecasting data. The reason why I need it in this format is that I can not plot 3 lines on that graph unless I get the transformed data into a new table and perform the calculations I mentioned. 

Please take a look at this thread at the bottom to see what issue I am trying to solve ultimately.
Solved: Re: How to perform 4 weeks rolling average for fis... - Microsoft Power BI Community
I really need a regular table if that can be created in a format I was looking for. 

OK, you can calculate the yearly 4 week rolling average with your data as is:

jdbuchanan71_0-1617937919884.png

This is the measure for 2020:

2020 4w ra = 
VAR _Weeks = 4
VAR _FiscalWeek = SELECTEDVALUE ( FiscalCalendar[FiscalWeek] )
RETURN
    IF (
        NOT ISBLANK ( _FiscalWeek ),
        DIVIDE (
            CALCULATE (
                [Revenue Amount],
                CALCULATETABLE (
                    DATESINPERIOD (
                        FiscalCalendar[Date],
                        MAX ( FiscalCalendar[Date] ),
                        - ( _Weeks * 7 ),
                        DAY
                    ),
                    FiscalCalendar[FiscalYear] = 2020
                ),
                VALUES ( FiscalCalendar[FiscalYear] )
            ),
            _Weeks
        )
    )

But, if you are dead set on having a new table you can create it using this code.

New Table = 
ADDCOLUMNS(
    SUMMARIZE(FiscalCalendar,FiscalCalendar[FiscalWeek]),
    "2020",CALCULATE(SUM(RevenueByDate[Revenue]),FiscalCalendar[FiscalYear] = 2020),
    "2021",CALCULATE(SUM(RevenueByDate[Revenue]),FiscalCalendar[FiscalYear] = 2021)
)

jdbuchanan71_1-1617938029181.png

I have updated the attached file showing both solutions.

 

 

 

@jdbuchanan71 
Thank you This is exactly what i needed. And you made it very understandable. 🙂

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.