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
HappyCamper
Frequent Visitor

How to compare apples to apples YTD, when some months are missing sales?

Hello Power BI Friends!

 

First time poster, long time fan of the advice given! 😄

Several clients that reported sales last year are no longer purchasing this year, so I want to be able to see an adjusted YTD vs. Last Year that accurately removes values of last year if no value is in current year. 

 

I know its either a sumif, while/then, or a loop formula... but honestly, I have no idea how to build this formula... any help would be greatly appreciated! 

 

For example:

Power BI.PNG

 

1 ACCEPTED SOLUTION

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

16 REPLIES 16
smpa01
Super User
Super User

@HappyCamper  it is an extremely interesting problem and even though @Ashish_Mathur  provided a great solution, I tried my hand on this as I approached the problem little differently.

 

A. Firstly I calculated what are the common months between max year in the data (2019 in this case) and non-max year (<2019 in this case).  If there is no sale in max year for those common months by Customer (sold-to), Calendar month, Brand, Line, Subline, SKU then sale=0 in non max year by Customer (sold-to), Calendar month, Brand, Line, Subline, SKU (I tried to attend to the last level of granularity to alter the sale to 0 on the condition specified)

Calendar month

APR
FEB
JAN
JUN
MAR
MAY
AUG
JUL

B. What are the months that are in non max year but not in max year. The correponding values are modified to 0 for non max year rows for Customer (sold-to), Calendar month, Brand, Line, Subline, SKU  as the end goal is to compare apple to apples

Calendar month

DEC
NOV
OCT
SEP

 

C. Keep the sale unmodified in max year.

 

I have done this by a DAX query

    Table =
VAR _11 =
    MAX ( 'Raw Data'[Calendar Year] ) ///Calculate Max year///
VAR _121 =
    SUMMARIZECOLUMNS (
        'Raw Data'[Calendar month],
        FILTER ( ALL ( 'Raw Data'[Calendar Year] ), 'Raw Data'[Calendar Year] = _11 ),
        "@Month", DISTINCT ( 'Raw Data'[Calendar month] )
    ) //What are the months in Max Year//
VAR _131 =
    DISTINCT ( SELECTCOLUMNS ( _121, "Calendar month", [@Month] ) ) //What are the months in Max Year//
VAR _141 =
    SUMMARIZECOLUMNS (
        'Raw Data'[Calendar month],
        FILTER ( ALL ( 'Raw Data'[Calendar Year] ), 'Raw Data'[Calendar Year] < _11 ),
        "@Month", DISTINCT ( 'Raw Data'[Calendar month] )
    ) //What are the months in Non-Max Year//
VAR _151 =
    DISTINCT ( SELECTCOLUMNS ( _141, "Calendar month", [@Month] ) ) //What are the months in Non-Max Year//
VAR _161 =
    EXCEPT ( _151, _131 ) // Are there any months that are in Non-Max year but not in max year//
VAR _21 =
    SUMMARIZECOLUMNS (
        'Raw Data'[Customer (sold-to)],
        'Raw Data'[Calendar Year],
        'Raw Data'[Calendar month],
        'Raw Data'[Brand],
        'Raw Data'[Line],
        'Raw Data'[Subline],
        'Raw Data'[SKU],
        FILTER ( ALL ( 'Raw Data'[Calendar Year] ), 'Raw Data'[Calendar Year] < _11 ),
        "@Sales", SUM ( 'Raw Data'[Sell Out Value] )
    ) // Summarize sales by non-max Year//
VAR _t0 =
    NATURALINNERJOIN (
        SELECTCOLUMNS (
            _21,
            "Customer (sold-to)", [Customer (sold-to)],
            "Calendar Year", [Calendar Year],
            "Calendar month", [Calendar month] & "",
            "Brand", [Brand],
            "Line", [Line],
            "Subline", [Subline],
            "Sales", [@Sales],
            "SKU", [SKU] & ""
        ),
        SELECTCOLUMNS ( _161, "Calendar month", [Calendar month] & "" )
    ) // Summarize sales by non-max Year only for _161 months//
VAR _t1 =
    SELECTCOLUMNS (
        _t0,
        "Calendar Year", [Calendar Year],
        "Calendar month", [Calendar month] & "",
        "Customer (sold-to)", [Customer (sold-to)],
        "Brand", [Brand],
        "Line", [Line],
        "Subline", [Subline],
        "SKU", [SKU],
        "Sales", 0
    )
VAR _71 =
    SUMMARIZECOLUMNS (
        'Raw Data'[Customer (sold-to)],
        'Raw Data'[Calendar Year],
        'Raw Data'[Calendar month],
        'Raw Data'[Brand],
        'Raw Data'[Line],
        'Raw Data'[Subline],
        'Raw Data'[SKU],
        FILTER ( ALL ( 'Raw Data'[Calendar Year] ), 'Raw Data'[Calendar Year] = _11 ),
        "@Sales", SUM ( 'Raw Data'[Sell Out Value] )
    ) // Summarize sales by max Year//
VAR _t2 =
    SELECTCOLUMNS (
        _71,
        "Calendar Year", [Calendar Year],
        "Calendar month", [Calendar month] & "",
        "Customer (sold-to)", [Customer (sold-to)],
        "Brand", [Brand],
        "Line", [Line],
        "Subline", [Subline],
        "SKU", [SKU],
        "Sales", [@Sales]
    )
VAR _1711 =
    NATURALINNERJOIN (
        SELECTCOLUMNS (
            _21,
            "Customer (sold-to)", [Customer (sold-to)],
            "Calendar Year", [Calendar Year],
            "Calendar month", [Calendar month] & "",
            "Brand", [Brand],
            "Line", [Line],
            "Subline", [Subline],
            "SKU", [SKU],
            "@Sales", [@Sales]
        ),
        SELECTCOLUMNS ( _131, "Calendar month", [Calendar month] & "" )
    ) // Summarize sales by non-max Year only for common months//
VAR _1712 =
    ADDCOLUMNS (
        _1711,
        "UQID", CONCATENATE (
            [Customer (sold-to)],
            CONCATENATE (
                [Calendar month],
                CONCATENATE (
                    [Brand],
                    CONCATENATE (
                        [Line],
                        CONCATENATE ( [Subline], CONCATENATE ( [SKU], [@Sales] ) )
                    )
                )
            )
        )
    ) //cretae unique identity//
VAR _1713 =
    SUBSTITUTEWITHINDEX ( _1712, "Index", _1711, 0 ) //inex by uqid//
VAR _1714 =
    NATURALLEFTOUTERJOIN ( _1712, _1713 ) //put back index to the original table//
VAR _1715 =
    SELECTCOLUMNS (
        _1714,
        "Customer (sold-to)", [Customer (sold-to)],
        "Calendar month", [Calendar month],
        "Brand", [Brand],
        "Line", [Line],
        "Subline", [Subline],
        "SKU", [SKU]
    ) // selecting few columns from non2019t//
VAR _1716 =
    SELECTCOLUMNS (
        _71,
        "Customer (sold-to)", [Customer (sold-to)],
        "Calendar month", [Calendar month],
        "Brand", [Brand],
        "Line", [Line],
        "Subline", [Subline],
        "SKU", [SKU]
    ) // selecting few columns from max year table//
VAR _1717 =
    EXCEPT ( _1715, _1716 ) //which are in non-max year but not in max year//
VAR _1718 =
    SELECTCOLUMNS (
        _1714,
        "Index", [Index],
        "Calendar Year", [Calendar Year],
        "Customer (sold-to)", [Customer (sold-to)],
        "Calendar month", [Calendar month],
        "Brand", [Brand],
        "Line", [Line],
        "Subline", [Subline],
        "SKU", [SKU]
    ) // selecting few columns from non2019t//
VAR _1719 =
    NATURALINNERJOIN ( _1717, _1718 )
VAR _t3 =
    SELECTCOLUMNS (
        _1719,
        "Calendar Year", [Calendar Year],
        "Calendar month", [Calendar month] & "",
        "Customer (sold-to)", [Customer (sold-to)],
        "Brand", [Brand],
        "Line", [Line],
        "Subline", [Subline],
        "SKU", [SKU],
        "Sales", 0
    )
VAR _1720 =
    EXCEPT (
        SELECTCOLUMNS ( _1713, "Index", [Index] + 0 ),
        SELECTCOLUMNS ( _1719, "Index", [Index] + 0 )
    )
VAR _1721 =
    NATURALINNERJOIN ( _1720, _1714 )
VAR _t4 =
    SELECTCOLUMNS (
        _1721,
        "Calendar Year", [Calendar Year],
        "Calendar month", [Calendar month] & "",
        "Customer (sold-to)", [Customer (sold-to)],
        "Brand", [Brand],
        "Line", [Line],
        "Subline", [Subline],
        "SKU", [SKU],
        "Sales", [@Sales]
    )
VAR _FinalTable =
    UNION ( _t1, _t2, _t3, _t4 )
RETURN
    _FinalTable

 

High_LevelHigh_Level

GranularGranular

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hello @Ashish_Mathur  & @smpa01 & @amitchandak & @Grambi ,

 

Thank you both so much! I am truly grateful for your support on this matter! I am sincerely impressed with this community and it makes me want to become a Power BI Pro. 😄

 

@smpa01 , yes, you are exactly right, your tables are exactly what I need. I have created the table in my power BI, however I am now getting the error "The expression refers to mulitple columns. Multiple columns cannot be converted to a scalar value." Any ideas?

 

Are you able to share the power bi you built with this data?

 

Thank you all again!

@HappyCamper  I am not sure what causes that but the DAX query is working absolutely fine on my end. You need to create a new table in order this query to run

 

Capture.PNG

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Ashish_Mathur
Super User
Super User

Hi,

Please clearly explain how you arrived at the numbers in the green box of the second image?  Share the logic very clearly.  Also, share data in a format that i can paste in an MS Excel workbook.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hello @Ashish_Mathur 

 

Thank you in advance for your support on this! I sincerely appreciate it! 😄 

 

So, I am looking to get the adjusted sales comparison. Meaning, if the current month's sales are zero, than I want last year's month sales to equal 0 (Even if there are sales in the LY sales)

 

The bottom table was showing how I wanted to calculate the adjusted YTD 

As you can see, in 2019, clients 1 and 2 are missings sales for a few months. So, I changed last year's month sales to 0 to match 2019. 

 

Does this make sense?

 

 2018       2019      
 JanFebMarAprMayJunJul JanFebMarAprMayJunJul
Client 19,9357,6578,7189,57112,54313,1596,811 10,19621,80031,43838,873000
Client 27,53411,37718,44611,68514,49712,15814,367 14,60810,86200000
Client 342,14143,73830,49033,56642,27734,99323,446 32,59926,41425,25123,34840,45135,20923,878
Client 48,9588,5467,7729,02110,9647,9518,490 7,6376,4105,8097,4593,8214,9452,238
Client 522,02915,15716,02718,37521,55116,22818,227 19,58115,30514,72127,91910,62112,85912,293
Client 620,52814,82434,22120,11122,17618,84017,841 11,72015,69917,27130,29729,99919,25722,789
Client 722,26723,64618,43126,65737,55143,26822,984 19,07323,98721,28126,60923,81226,26121,837
Total133,392124,944134,105128,985161,559146,597112,166 115,414120,477115,771154,505108,70498,53183,035
                
 Current Formula            
             
 YTD 2018YTD 2019%             
 941,748796,437-15%            
                
                
 2018       2019      
 JanFebMarAprMayJunJul JanFebMarAprMayJunJul
Client 19,9357,6578,7189,571000 10,19621,80031,43838,873000
Client 27,53411,37700000 14,60810,86200000
Client 342,14143,73830,49033,56642,27734,99323,446 32,59926,41425,25123,34840,45135,20923,878
Client 48,9588,5467,7729,02110,9647,9518,490 7,6376,4105,8097,4593,8214,9452,238
Client 522,02915,15716,02718,37521,55116,22818,227 19,58115,30514,72127,91910,62112,85912,293
Client 620,52814,82434,22120,11122,17618,84017,841 11,72015,69917,27130,29729,99919,25722,789
Client 722,26723,64618,43126,65737,55143,26822,984 19,07323,98721,28126,60923,81226,26121,837
Total133,392124,944115,659117,300134,519121,28090,988 115,414120,477115,771154,505108,70498,53183,035
                
                
                
 Formula I want            
             
 YTD 2018YTD 2019%             
 838,082796,437-5%            
                

 

 

Please find the solution in pbix

https://www.dropbox.com/s/hm8phcckyxcolq2/MissingmonthAdjustment.pbix?dl=0

Sales YTD on LYTD = 
Var   _start_date=(minx('Date',STARTOFYEAR('Date'[Date]))) 
Var   _end_date=(max('Date'[Date]))

Var _last_year_mtd_val= CALCULATE(sum(Sales[Sales Amount]),Sales[Sales Date] >= _start_date && (Sales[Sales Date]) <= _end_date,filter(Sales,COUNTROWS(SAMEPERIODLASTYEAR(Sales[Sales Date]))>0))
return
_last_year_mtd_val

 

 


@amitchandak wrote:

Please find the solution in pbix

https://www.dropbox.com/s/hm8phcckyxcolq2/MissingmonthAdjustment.pbix?dl=0

Sales YTD on LYTD = 
Var   _start_date=(minx('Date',STARTOFYEAR('Date'[Date]))) 
Var   _end_date=(max('Date'[Date]))

Var _last_year_mtd_val= CALCULATE(sum(Sales[Sales Amount]),Sales[Sales Date] >= _start_date && (Sales[Sales Date]) <= _end_date,filter(Sales,COUNTROWS(SAMEPERIODLASTYEAR(Sales[Sales Date]))>0))
return
_last_year_mtd_val

 

 


@HappyCamper, this solution did not work out ?

Hi,

Share data in a form that i can paste in an Excel workbook.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

https://app.box.com/s/fsyu5yih1x0sslr1yiqq3hb6gob46u8y

 

Hello @Ashish_Mathur ,

 

Please let me know if you can open this file from my box account.

 

Thank you!

I understand what you want. Now please share the format in which your actual raw data is arranged.

Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hello @Ashish_Mathur ,

 

Below is the link to my box.com raw data file.

 

The columns include: Customer, Year, Month, Brand, Line, Subline, SKU, and Sales Value.

 

This is how the data is pulled from the system.  

 

https://app.box.com/s/sn32fmn25iestqcfuzx8bg54848fjzl1

 

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks for teaching that calculation based on SUMMARIZE @Ashish_Mathur . Very insightful Smiley Very Happy

 

With a little change it's possible to arrange the table visual so it kind of reflects the examples given by HappyCamper, and shows per client and month what part of the sales from last year is or isn't adjusted.

 

Schermafbeelding 2019-10-05 om 15.12.51.png

 

I would need to change the measures Sales this year and Sales last year, so they can be displayed per month. Or add new ones.

 

sumx(filter('Raw Data';year('Raw Data'[Date])=year(today()));'Raw Data'[Sell Out Value])
sumx(filter('Raw Data';year('Raw Data'[Date])=year(today())-1);'Raw Data'[Sell Out Value])

 

To go from the default table in your pbix to the one shown above:

  • Remove the field Year from the columns
  • Add Sales this year, Sales last year, Adjusted sales and Growth to the Values, remove Sell Out from the values.

You are welcome @Grambi 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
amitchandak
Super User
Super User

In year current year calc you have to add a filter for last year sales. Something like this

Sales YTD = 
Var   _start_date=year(max('Date'[Date Filer]))
Var   _end_date=year(min(STARTOFYEAR('Date'[Date Filer])))

Var _last_year_mtd_val= CALCULATE(sum(Sales[Sales Amount]),Sales[Sales Date] >= _start_date && (Sales[Sales Date]) <= _end_date, filter(Sales,CALCULATE(sum(Sales[Sales Amount]),SAMEPERIODLASTYEAR(Sales[Sales Date]))>0))
return
_last_year_mtd_val

 

Hello @amitchandak

 

Thank you so much for all the support! I am going to work on the file now, I will let you know.

 

But I sincerely appreciate your reply! 😄

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.