Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Percentage YOY calculation based on columns available in Table

HI I have to calcualte % values for each year with the combination of Company available in a Pivot table and that measure should calcuate dynamically when i pull in Pivot table.

 

Ex: for year 2019 ( apple ) =  (Apple 2019 Headcount ) - (Apple 2018 Headcount) / (Apple 2018 Headcount )

                                           =  ( 106700 - 105800 ) / 105800       --> Nearly 1% 

                   2018 ( apple ) =  (Apple 2018 Headcount ) - (Apple 2017 Headcount) / (Apple 2017 Headcount )

                                          =  (105800 - 106400) / 106400          --> Nearly  -1%

                  2017 ( apple ) =  (Apple 2017 Headcount ) - (Apple 2016 Headcount) / (Apple 2016 Headcount )

                   2016 ( apple) = ( Should be blank of not having 2015 Head count data ) 

 

Simillary, same calucation should happend for NOkia, Samsung.

 

How to do that dynamic calcuation with only one measure i need to create ?

 

Actaully i'm unabl to upload the PBIX file, didnt see option to upload but the data is this only .

 

CompanyYearHeadcount
Apple2019106700
Apple2018105800
Apple2017106400
Apple2016102500
Samsung201992005
Samsung2018103564
Samsung2017105870
Samsung2016104553
Nokia2019146768
Nokia2018150711
Nokia2017152000
Nokia2016155000

 

THanks,

Rams

  • Anonymous 

     

    Try this DAX measure

     

    Percentage YOY =
    VAR _currentYear =
        SELECTEDVALUE ( 'Table'[Year] )
    VAR _company =
        SELECTEDVALUE ( 'Table'[Company] )
    VAR _current =
        SUM ( 'Table'[Headcount] )
    VAR _prior =
        SUMX (
            FILTER (
                ALL (
                    'Table'[Company],
                    'Table'[Headcount],
                    'Table'[Year]
                ),
                ( 'Table'[Year] = _currentYear - 1 )
                    && ( 'Table'[Company] = _company )
            ),
            'Table'[Headcount]
        )
    VAR _yoy =
        DIVIDE (
            _current - _prior,
            _prior,
            BLANK ()
        )
    RETURN
        _yoy

     



    Did I answer your question? Mark my post as a solution!
    Appreciate with a kudos
    🙂

  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Anonymous ,

     

    You can use the below measure

     

    Previous Year Headcount % =
    Var _max = MAX ('Table'[Year])
    Var _previousYear = CALCULATE( MAX('Table'[Year]), FILTER(ALL('Table'), 'Table'[Year] < _max && 'Table'[Company] = MAX('Table'[Company])))
    Var _previousCompany = CALCULATE( MAX('Table'[Company]), FILTER(ALL('Table'), 'Table'[Year] < _max && 'Table'[Company] = MAX('Table'[Company])))
    var _previousYearHeadcount = CALCULATE( MAX('Table'[Headcount]), FILTER(ALL('Table'), 'Table'[Year] = _previousYear && 'Table'[Company] = _previousCompany))

    RETURN

    DIVIDE(MAX('Table'[Headcount]) - _previousYearHeadcount,_previousYearHeadcount)
     
     
     

    Regards,
    Harsh Nathani

    Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

10 Replies

  • nandukrishnavs's avatar
    nandukrishnavs
    Icon for Community Champion rankCommunity Champion

    Anonymous 

     

    Try this DAX measure

     

    Percentage YOY =
    VAR _currentYear =
        SELECTEDVALUE ( 'Table'[Year] )
    VAR _company =
        SELECTEDVALUE ( 'Table'[Company] )
    VAR _current =
        SUM ( 'Table'[Headcount] )
    VAR _prior =
        SUMX (
            FILTER (
                ALL (
                    'Table'[Company],
                    'Table'[Headcount],
                    'Table'[Year]
                ),
                ( 'Table'[Year] = _currentYear - 1 )
                    && ( 'Table'[Company] = _company )
            ),
            'Table'[Headcount]
        )
    VAR _yoy =
        DIVIDE (
            _current - _prior,
            _prior,
            BLANK ()
        )
    RETURN
        _yoy

     



    Did I answer your question? Mark my post as a solution!
    Appreciate with a kudos
    🙂

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi nandukrishnavs,,amitchandak ,Anonymous 

      Thanks for the promt Help with multiple measures :), But in my case all are coming from one table so i tried what Nandu mentioned, but i'm getting nulls, may be i missed complete information what i need.

       

      Below is the once which i have showing the screenshot,but i created in Excle, which i'm uanble to write in DAX

      Which i want the same as below in Power BI

       

      One with actual values table, and one with % values table ( not having any Actual values )

       

      😞 this is what my second graph should show lik, but i created that in excel, 

      In my case i want that should be written in Dax 😞 

       

      Thanks,

      Rams 

      • nandukrishnavs's avatar
        nandukrishnavs
        Icon for Community Champion rankCommunity Champion

        Anonymous  You have to create a DAX measure. I think you have created a calculated column.

        I tried your sample data, it is working for me. You can find the snapshot in my original post.

        Did I answer your question? Mark my post as a solution!
        Appreciate with a kudos
        🙂

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    You can use the below measure

     

    Previous Year Headcount % =
    Var _max = MAX ('Table'[Year])
    Var _previousYear = CALCULATE( MAX('Table'[Year]), FILTER(ALL('Table'), 'Table'[Year] < _max && 'Table'[Company] = MAX('Table'[Company])))
    Var _previousCompany = CALCULATE( MAX('Table'[Company]), FILTER(ALL('Table'), 'Table'[Year] < _max && 'Table'[Company] = MAX('Table'[Company])))
    var _previousYearHeadcount = CALCULATE( MAX('Table'[Headcount]), FILTER(ALL('Table'), 'Table'[Year] = _previousYear && 'Table'[Company] = _previousCompany))

    RETURN

    DIVIDE(MAX('Table'[Headcount]) - _previousYearHeadcount,_previousYearHeadcount)
     
     
     

    Regards,
    Harsh Nathani

    Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous 

       

      I tried with this as well, but getting all 0.50 Valus 😞 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Anonymous ,

         

        Please create a measure not a Calculated Column.

         

         

         

        Regards,

        Harsh Nathani

  • Anonymous , if you do not have date, then make sure you year is in separate table and try

    This Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
    Last Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))
    diff = [This Year] -[Last Year]
    Diff % = divide( [This Year] -[Last Year],[Last Year])
    //if you have date with date calendar
    
    YTD QTY = TOTALYTD(Sum('order'[Qty]),'Date'[Date])
    LYTD QTY = TOTALYTD(Sum('order'[Qty]),dateadd('Date'[Date],-1,year))
    Previous Year = CALCULATE(SUM('order'[Qty]), PREVIOUSYEAR('Date'[Date]))
    
    
    YTD QTY forced= 
    var _max = today()
    return
    calculate(Sum('order'[Qty]),DATESYTD('Date'[Date]),'Date'[Date]<=_max)
    //calculate(TOTALYTD(Sum('order'[Qty]),'Date'[Date]),filter('Date','Date'[Date]<=_max))
    
    LYTD QTY forced= 
    var _max = date(year(today())-1,month(today()),day(today()))
    return
    CALCULATE(Sum('order'[Qty]),DATESYTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max)
    //TOTALYTD(Sum('order'[Qty]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)

     

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
    https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
    https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
    https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/


    Please Watch/Like/Share My webinar on Time Intelligence: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184
    My Youtube Tips at: https://www.youtube.com/playlist?list=PLPaNVDMhUXGYrm5rm6ME6rjzKGSvT9Jmy

    Appreciate your Kudos.