Forum Discussion

midrississi's avatar
midrississi
Frequent Visitor
5 years ago
Solved

Calculated column based on formula, filter, month and year - using slicer

Hello everybody,

I'd like to ask you for help with a calculated column,
I'll use the following table to demostrate it on an example,

 

Table

 

In order to get the Requested column NEW_PRICE I use this formula (in this column I want to always have the PRICE of the last month of the year for all the other months) :

NEW_PRICE =
CALCULATE(MAX('Table'[PRICE]),
FILTER('Table',EARLIER('Table'[NO_YEAR])='Table'[NO_YEAR] &&
EARLIER('Table'[ID_ITEM])='Table'[ID_ITEM] &&
'Table'[NO_MONTH]=MAXX(FILTER('Table',EARLIER('Table'[NO_YEAR])='Table'[NO_YEAR]),'Table'[NO_MONTH])
)
)
 
It works, however, when I use the slicer for the visualization, the date slicer doesn't have any influence in my new column and it still returns the value of the whole table.
for exemple, if my slicer indicate values between 01/01/2018 and 01/10/2019, my new column show always the new price of the last month (01/12/2019) which is 19 instead of taking the PRICE of the new last month which is 23.
 
Is there anything I can do about that? Any formula that cooperates with the date and will return a value only for the time period I filter using the slicer?
 
Thanks in advance
  • AlB's avatar
    AlB
    5 years ago

    midrississi 

     

    Try this. If it doesn't work I'd need the pbix to run some tests (or a pbix with dummy data that repros the issues)

    New price = 
    VAR lastMonth_ =
        CALCULATE ( MAX ( 'Table'[NO_MONTH] ),
        ALLSELECTED(),
        SUMMARIZE('Table','Table'[ID_ITEM],'Table'[NO_YEAR] )
        )
    
    VAR priceMoisAnnee_ =
            CALCULATE (
            MAX ('Table'[PRICE]),
            'Table'[NO_MONTH] = lastMonth_,
            ALLSELECTED(),
            SUMMARIZE('Table','Table'[ID_ITEM],'Table'[NO_YEAR] )        
        )
    RETURN
        priceMoisAnnee_ 

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

9 Replies

    • midrississi's avatar
      midrississi
      Frequent Visitor

      Hi amitchandak,
      thank you for your reply. But is there another solution using a calculated measure, for example, to meet this need?
      thank you in advance for your return

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

    Hi midrississi 

    Please always show your sample data in text-tabular format in addition to (or instead o) the screen captures. A screen cap helps, like in this case, but doesn't allow people to readily copy the data and run a quick test and thus decreases the likelihood of your question being answered. Just use 'Copy table' in Power BI and paste it here.

     

    Don't create a calculated column. That is static and once created will not change, certainly not under a slicer. Create a measure instead:

    New price =
    VAR lastDate_ =
        CALCULATE ( MAX ( Table1[DT_Day] ), ALLEXCEPT ( Table1, Table1[ID_Item] ) )
    VAR priceLastDate_ =
        CALCULATE (
            MAX ( Table1[Price] ),
            Table1[DT_Day] = lastDate_,
            ALLEXCEPT ( Table1, Table1[ID_Item] )
        )
    RETURN
        priceLastDate_

    Place that measure in a table visual just like you had it, with all those five fields in the table

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

    • midrississi's avatar
      midrississi
      Frequent Visitor

      Hi AlB ,

       

      Thank you for the help, I tried to apply your code in my case,  it gives the true values, but the slicer doesn't have any effect  in my new measure added to the table when I change the date.

      this is the code I used based on your code, still don't have the effect of slicer on.

       

      New price = 
      VAR lastMonth_ =
          CALCULATE ( MAX ( 'Table'[NO_MONTH] ),
          ALLSELECTED('Table'[DT_DAY]),
          ALLEXCEPT ( 'Table','Table'[ID_ITEM],'Table'[NO_YEAR] )
          )
      
      VAR priceMoisAnnee_ =
              CALCULATE (
              MAX ('Table'[PRICE]),
              'Table'[NO_MONTH] = lastMonth_,
              ALLEXCEPT ( 'Table','Table'[ID_ITEM],'Table'[NO_YEAR] )
              
          )
      RETURN
          priceMoisAnnee_ 

       

      this is the result before applying any change on slicer (the result was good)

      ID_ITEMDT_DAYNO_YEARNO_MONTHPRICENEW_PRICE
      101/01/2018201811922
      101/02/2018201822022
      101/03/2018201832322
      101/04/2018201842322
      101/05/2018201852222
      101/06/2018201861922
      101/07/2018201872222
      101/08/2018201882422
      101/09/2018201892022
      101/10/20182018101822
      101/11/20182018112522
      101/12/20182018122222
      101/01/2019201912219
      101/02/2019201922019
      101/03/2019201932019
      101/04/2019201941919
      101/05/2019201952519
      101/06/2019201962319
      101/07/2019201971919
      101/08/2019201982019
      101/09/2019201992319
      101/10/20192019102319
      101/11/20192019112219
      101/12/20192019121919
      101/01/2020202012220
      101/02/2020202022020
      101/03/2020202032120
      101/04/2020202042020

       but after using slicer the result doesn't change. This is the screen of my result :

       

      Thank you in advance.

      Best Regards,

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

        midrississi 

         

        Try this. If it doesn't work I'd need the pbix to run some tests (or a pbix with dummy data that repros the issues)

        New price = 
        VAR lastMonth_ =
            CALCULATE ( MAX ( 'Table'[NO_MONTH] ),
            ALLSELECTED(),
            SUMMARIZE('Table','Table'[ID_ITEM],'Table'[NO_YEAR] )
            )
        
        VAR priceMoisAnnee_ =
                CALCULATE (
                MAX ('Table'[PRICE]),
                'Table'[NO_MONTH] = lastMonth_,
                ALLSELECTED(),
                SUMMARIZE('Table','Table'[ID_ITEM],'Table'[NO_YEAR] )        
            )
        RETURN
            priceMoisAnnee_ 

         

        Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

        Contact me privately for support with any larger-scale BI needs, tutoring, etc.

        Cheers