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

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

 

2.png

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.
3.png
4.png
 
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
1 ACCEPTED SOLUTION

@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 

 

SU18_powerbi_badge

View solution in original post

9 REPLIES 9
AlB
Super User
Super User

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 

 

SU18_powerbi_badge

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 :

11.png

 

Thank you in advance.

Best Regards,

@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 

 

SU18_powerbi_badge

@AlB ,

 

The same thing the code gives the good result before filter but after filter I don't have what I want.
How I can share with you the .pbix project ?

 

Thank you in advance.

Best regards,

@midrississi 

You have to share here the URL to the file hosted elsewhere: Dropbox, Onedrive... or just upload the file to a site like tinyupload.com (no sign-up required).

 

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 

 

SU18_powerbi_badge

@AlB ,

 

Thank you for finding the URL to download the .pbix project.

URL : http://s000.tinyupload.com/?file_id=00669915136941173932

 

Thank you in advance for your help.

Best regards,

@AlB 

It's okay I found where the problem comes from.
Instead of using the "DT_DATE" field which is in the fact table to filter in the slicer, I used the one which is in the Date dimension table and it worked.

 

Thanks for your help.

Best regards

amitchandak
Super User
Super User

@midrississi , You can not use slicer value in a new column.

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

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.

Top Solution Authors