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
mb0307
Responsive Resident
Responsive Resident

Table showing variance by product, week and month

Hi all,

Thanks you in advance if someone can help me with my query below.  I am new to DAX.

 

My table example:

PRODUCTFORECAST WEEKMONTHSALES
ClockWeek 1August100
ClockWeek 1September500
FanWeek 1August200
FanWeek 1September200
ClockWeek 2August150
ClockWeek 2September400
FanWeek 2August200
FanWeek 2September300

Above table, shows FORECAST WEEK in which sales numbers were projected for August and September.  As we move to different weeks, our forcast figures are changing for each month.

 

SUMMARY of above table is:

 Week 1 Week 2 
PRODUCTAugustSeptemberAugustSeptember
Clock100500150400
Fan200200200300

 

Is it possible using DAX to create a table in this visual format please?

 

RESULT table should show the PRODUCT and difference between forcasted sales for each month:

Week 1 vs Week 2
PRODUCTAugustSeptember
Clock50-100
Fan0100

 

Thanks

1 ACCEPTED SOLUTION

Not sure exactly what you mean, but something is returning blank somewhere.  You can make the expression more robust with the following change

 

Forecast Difference =
VAR __thisweek =
    MAX ( Forecast[FORECAST WEEK] )
VAR __thisweekforecast =
    CALCULATE ( SUM ( Forecast[SALES] ), Forecast[FORECAST WEEK] = __thisweek )
VAR __lastweekforecast =
    CALCULATE ( SUM ( Forecast[SALES] ), Forecast[FORECAST WEEK] = __thisweek - 1 )
RETURN
    IF (
        OR ( ISBLANK ( __thisweekforecast ), ISBLANK ( __lastweekforecast ) ),
        BLANK (),
        __thisweekforecast - __lastweekforecast
    )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Employee
Employee

Here is one way to solve this one.  First thing is to change your Forecast Week column to an integer (text after space delimiter in query editor), so you can subtract 1 from it easily.  Then you can use a measure like this in a matrix visual with Product (rows) and Month (columns).  It calculates the difference between the forecast is the latest week vs. the previous one.

 

Forecast Difference =
VAR __thisweek =
MAX ( Forecast[FORECAST WEEK] )
VAR __thisweekforecast =
CALCULATE ( SUM ( Forecast[SALES] ), Forecast[FORECAST WEEK] = __thisweek )
VAR __lastweekforecast =
CALCULATE ( SUM ( Forecast[SALES] ), Forecast[FORECAST WEEK] = __thisweek - 1 )
RETURN
__thisweekforecast - __lastweekforecast

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


mb0307
Responsive Resident
Responsive Resident

Thanks Pat.

 

I have Weeks in number format and logically I think is what I want.  But if a month has no SALES values (i.e. 0) in the selected weeks then it seems to ignore the week filter and calculate all weeks.  Can this be fixed please?

Not sure exactly what you mean, but something is returning blank somewhere.  You can make the expression more robust with the following change

 

Forecast Difference =
VAR __thisweek =
    MAX ( Forecast[FORECAST WEEK] )
VAR __thisweekforecast =
    CALCULATE ( SUM ( Forecast[SALES] ), Forecast[FORECAST WEEK] = __thisweek )
VAR __lastweekforecast =
    CALCULATE ( SUM ( Forecast[SALES] ), Forecast[FORECAST WEEK] = __thisweek - 1 )
RETURN
    IF (
        OR ( ISBLANK ( __thisweekforecast ), ISBLANK ( __lastweekforecast ) ),
        BLANK (),
        __thisweekforecast - __lastweekforecast
    )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.