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
Anonymous
Not applicable

Previous week value

Hi All,

 

I would like to create a measure value to get previous week value. I dont want to use today-7. Also the below data is for jan 2020 where the data is present in the weekly manner.

Previous week.PNG

Thanks in advance

7 REPLIES 7
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

First, you need to unpivot all your columns: Go to edit queries>Transform>select all the columns(except the column total)>unpivot columns,then you will as below:

14.png16.png

Then  add an index column from 1:

17.png

 

Finally return to the data view and a calculated column using a dax expression as below:

 

 

Column = CALCULATE(SUM('Table'[Value]),FILTER('Table','Table'[Index]=EARLIER('Table'[Index])-1))

 

 

And you will see:

18.png

 

For the related .pbix file,pls click here.

 


Best Regards,
Kelly

 
Anonymous
Not applicable

Hi @v-kelly-msft ,

 

Thanks for your suggestion but by doing unpivot it might affect my other column present in dataset. So is there any way to do it by some other way around.

 

Thanks

zoloturu
Memorable Member
Memorable Member

Hi @Anonymous ,

 

It can be done in several ways.

Let us know how do you know what is the number of previous week? Do you base on today date?


Regards,
Ruslan Zolotukhin (zoloturu)
BI Engineer at Akvelon Inc. / Kharkiv Power BI User Group Leader / DAX & Power BI Trainer
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

You are from Ukraine? If yes then welcome to Power BI User Group - KhPUG website. Other country? Check and find proper one - Power BI User Groups

 

 

Anonymous
Not applicable

Hi @zoloturu ,

 

No Not based on today date,

 

I would like to have like Weeknum = max(weeknum) - 1.

 

i.e., previous week data should display in current week data.

 

Thanks

@Anonymous ,

 

Try this one

Measure Week-1 =
VAR __current_week = ( 'TableName1'[Week] )
VAR __delimiter_position = ( "-", __current_week, 1, 0 )
VAR __previous_week_number = ( LEFT ( __current_week, __delimiter_position - 1 ) ) + 1
VAR __year = ( __current_week, 4 ) 
/* OR use __year = MAX('TableName1'[Year]) if separate column for year exists */
VAR __previous_week = __previous_week_number & "-" & __year

RETURN
    CALCULATE (
        SUM ( 'TableName2'[Sales'] ),
        FILTER ( ALL ( 'TableName1' ), 'TableName1'[Week] = __previous_week )
    )

You need to replace TableName1, TableName2 with proper table names from your report model.

 

Regards,
Ruslan Zolotukhin (zoloturu)
BI Engineer at Akvelon Inc. / Kharkiv Power BI User Group Leader / DAX & Power BI Trainer
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

You are from Ukraine? If yes then welcome to Power BI User Group - KhPUG website. Other country? Check and find proper one - Power BI User Groups 

Anonymous
Not applicable

Hi @zoloturu 

 

Could you please attach the Pbix file for the below code which you tried?

Measure Week-1 =
VAR __current_week = ( 'TableName1'[Week] )
VAR __delimiter_position = ( "-", __current_week, 1, 0 )
VAR __previous_week_number = ( LEFT ( __current_week, __delimiter_position - 1 ) ) + 1
VAR __year = ( __current_week, 4 ) 
/* OR use __year = MAX('TableName1'[Year]) if separate column for year exists */
VAR __previous_week = __previous_week_number & "-" & __year

RETURN
    CALCULATE (
        SUM ( 'TableName2'[Sales'] ),
        FILTER ( ALL ( 'TableName1' ), 'TableName1'[Week] = __previous_week )
    )

 I am gettting so many doubts thats why.

 

'TableName1'[Week] is a measure?
 
Thanks

Hi @Anonymous ,

 

I wrote this code without PBIX.

 

'TableName1'[Week] is your week column ("04-2020", "05-2020")


Regards,
Ruslan Zolotukhin (zoloturu)
BI Engineer at Akvelon Inc. / Kharkiv Power BI User Group Leader / DAX & Power BI Trainer
-------------------------------------------------------------------
Did I answer your question? Mark my post as a solution!
It was useful? Press Thumbs Up!

You are from Ukraine? If yes then welcome to Power BI User Group - KhPUG website. Other country? Check and find proper one - Power BI User Groups

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.