Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
ellevy
Helper I
Helper I

previous row calculations

Hi

 

i have a data of the following( for example)


year |  week in year | no. of people

2017        1                       500

2018        1                        300

2017        2                        100
2018        2                         50

*week in year is not derived from date but it based on some internal week counting in my organization

now - all i want to do is to put the current year vs previous year (with respeact to the same week in year no of people, meaning:

 

year   |  week in year      |   no of people  current year  |  no of pepole previous year

2018                  1                               300                                     500

2018                  2                                 50                                       100

 

so i could make a division calculation between those years with respect to this "week in year" coulmn ... but i can't make it! i tried with previousyear  function but it works only if i omit this "week in year" coulmn ...

 

i also wish to know how can i calc "previous year" if it was with a numric format and not date format (which then i can't use the previousyear function)

 

thanks!

Elad

 

 

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@ellevy try this measure for previous year

 

Prev Year and Week = 
VAR __currentYear = MAX ( Table[Year] )
VAR __prevYear = __currentYear - 1
VAR __currentWeek = MAX ( Table[Week in Year] )
RETURN
CALCULATE (
SUM ( Table[Number of People] ),
Table[Year] = __currentYear,
Table[Week in Year] = __currentWeek
) 

 

Check my latest blog post Year-2020, Pandemic, Power BI and Beyond to get a summary of my favourite Power BI feature releases in 2020

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

6 REPLIES 6
parry2k
Super User
Super User

@ellevy try this

 

VAR __currentYear = YEAR ( Table[DateColumn] )
VAR __prevYear = __currentYear - 1

 

Check my latest blog post Year-2020, Pandemic, Power BI and Beyond to get a summary of my favourite Power BI feature releases in 2020

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@ellevy not sure why it is not working for you, but works for me

 

parry2k_0-1610387781496.png

 

 

Prev = 
VAR __selectedYear = MAX ( PRow[Year] )
VAR __prevYear = __selectedYear - 1
VAR __selectedWeek = MAX ( PRow[Week] )
RETURN
CALCULATE (
    SUM ( PRow[People] ),
    PRow[Year] = __prevYear,
    PRow[Week] = __selectedWeek
)

Check my latest blog post Year-2020, Pandemic, Power BI and Beyond to get a summary of my favourite Power BI feature releases in 2020

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@ellevy try this measure for previous year

 

Prev Year and Week = 
VAR __currentYear = MAX ( Table[Year] )
VAR __prevYear = __currentYear - 1
VAR __currentWeek = MAX ( Table[Week in Year] )
RETURN
CALCULATE (
SUM ( Table[Number of People] ),
Table[Year] = __currentYear,
Table[Week in Year] = __currentWeek
) 

 

Check my latest blog post Year-2020, Pandemic, Power BI and Beyond to get a summary of my favourite Power BI feature releases in 2020

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi parry2k

 

this looks promise but it only return me the same no of people meaning :

year | week in year | no of people current year | no of pepole previous year

2018        1                                    300                              300

2018       2                                     400                              400

the "year" is in date format (so i think the year-1 is not working there....)



Ok i managed to make this work using :

CALCULATE (
SUM ( Table[Number of People] ),
Table[Year] = __PREVYEAR,
Table[Week in Year] = __currentWeek
) 

 

and i used a numric date column that i have (meaning integer format and not date format)

 

how can i do the:


VAR __prevYear = __currentYear - 1

using a date format column ?

amitchandak
Super User
Super User

@ellevy , Create a column

Week Year = [Year]*100 + [Week]

 

Create a week year table with year, week, and Week Year table.   Assume Date

 

Example measure

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))

 

For Week comparison you can create Week rank column in your table

Week Rank = RANKX(all('Date'),'Date'[Year Week],,ASC,Dense) //YYYYMM format

 

measures like

 

This Week = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])))
Last Week = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])-1))
Last year Week= CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=(max('Date'[Week Rank]) -52)))

 

 

Power BI — Year on Year with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
https://www.youtube.com/watch?v=km41KfM_0uA

Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...
https://www.youtube.com/watch?v=pnAesWxYgJ8

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.