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.
Hi, I'm a fairly new Power BI user and am having a problem calculating a measure for a Matrix visual.
My aim is to calculate the previous weeks miles for each business division in a Matrix. My current function calculates the total previous week miles. However, I need it to calculate the previous weeks miles for each business division.
Solved! Go to Solution.
HI @clarkey1988,
I think you also need to consider about the year conversion issue, so I added if statement your formula to dispatch to two conditions based on current week number that extract from your table date fields. (I add one additional condition to your formula to filter records based on the year and week number, it will be filtered more accurately if your table contains many year records)
You can try it if below formula meets to your requirement:
Previous Week =
VAR currDate =
MAX ( 'Trip Detail'[date] )
RETURN
CALCULATE (
SUM ( 'Trip Detail'[Miles] ),
FILTER (
ALLSELECTED ( 'Trip Detail' ),
IF (
weekNumber > 1,
YEAR ( 'Trip Detail'[Date] ) = YEAR ( currDate )
&& WEEKNUM ( 'Trip Detail'[Date] )
= WEEKNUM ( currDate ) - 1,
YEAR ( 'Trip Detail'[Date] )
= YEAR ( currDate ) - 1
&& WEEKNUM ( 'Trip Detail'[Date] )
= WEEKNUM ( DATE ( YEAR ( currDate ) - 1, 12, 31 ) )
)
),
VALUES ( 'Trip Detail'[Business Division] )
)
Regards,
Xiaoxin Sheng
Thank you all for your responses!
@clarkey1988 , Make sure week is in a separate table. In date table or week Table.
Refer my blog
You don't need ALLSELECTED. What is your current week measure?
You probably only need something along the lines of:
Previous week = CALCULATE(SUM('Trip Detail'[Miles]),
FILTER(ALL('Trip Detail'),
'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1))
If there are other row contexts (nested for example) you might need ALLEXCEPT
Something along the lines of:
Previous week = CALCULATE(SUM('Trip Detail'[Miles]),
(FILTER(ALLEXCEPT('Table, Table[Business Division]'),'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1)))
Proud to be a Super User!
Paul on Linkedin.
Hi @PaulDBrown
Thanks for responding. My current measure is simply the sum of the current weeks miles/sum of week 10 miles (pre COVID)
However, I need it to be week over week and not specific to week 10. I have two filters within the Matrix, but it seems the measure you listed without ALLSELECTED still sums the TOTAL miles, and does not break it out by business division. The business division is a column I added and made into an if statement.
@clarkey1988
Hi again,
You probably need to include the fweek number in the ALL clause:
Previous week = CALCULATE(SUM('Trip Detail'[Miles]),
FILTER(ALL('Trip Detail'[Week Number]),
'Trip Detail'[Week Number]=MAX('Trip Detail'[Week Number])-1))
Otherwise try with the second option I posted using ALLEXCEPT, see if that works.
Proud to be a Super User!
Paul on Linkedin.
@PaulDBrown so I think I'm close. The first optuion did not work
The ALLEXCEPT version below calculatues the previous weeks miles by business division accurately. However, it does not take into considertation the two filters I have in the matrix.
HI @clarkey1988,
I think you also need to consider about the year conversion issue, so I added if statement your formula to dispatch to two conditions based on current week number that extract from your table date fields. (I add one additional condition to your formula to filter records based on the year and week number, it will be filtered more accurately if your table contains many year records)
You can try it if below formula meets to your requirement:
Previous Week =
VAR currDate =
MAX ( 'Trip Detail'[date] )
RETURN
CALCULATE (
SUM ( 'Trip Detail'[Miles] ),
FILTER (
ALLSELECTED ( 'Trip Detail' ),
IF (
weekNumber > 1,
YEAR ( 'Trip Detail'[Date] ) = YEAR ( currDate )
&& WEEKNUM ( 'Trip Detail'[Date] )
= WEEKNUM ( currDate ) - 1,
YEAR ( 'Trip Detail'[Date] )
= YEAR ( currDate ) - 1
&& WEEKNUM ( 'Trip Detail'[Date] )
= WEEKNUM ( DATE ( YEAR ( currDate ) - 1, 12, 31 ) )
)
),
VALUES ( 'Trip Detail'[Business Division] )
)
Regards,
Xiaoxin Sheng
Hi @v-shex-msft , your answer helped with my Last Calendar week Orders calculation.I am struggling to calculate orders for Last 4 Calendar weeks. Any help with that will be greatly appreciated.
Ok, so we are getting close...
Something which I din't ask, and is actually very important (for many reasons, including these kind of calculations): do you have a calendar or Date table?
If not, please create one and link it to your tables in one-to-many relationship.
(As regards the measure not taking into account the filters, it's hard to see what's going on without knowing what/if tables & relationships you are enacting in the filters...).
Ps, it would be very helpful if you could provide a sample PBIX file/sample data (with fake data of need be) to work on!
Proud to be a Super User!
Paul on Linkedin.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
110 | |
97 | |
78 | |
64 | |
55 |
User | Count |
---|---|
143 | |
109 | |
89 | |
84 | |
66 |