Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
Hello All ,
This is a question which has been a problem for atleast a month . I am using Power BI for more than a year. But I am stuck here and any help will be highly appreciated
Following is the dummy datatable
State | SaleID | Sale Date | Total Sale | Service Period (in months) |
A | 1 | 1/1/2023 | 249 | 13 |
A | 2 | 1/1/2022 | 317 | 15 |
B | 3 | 1/1/2020 | 291 | 20 |
B | 4 | 1/1/2021 | 388 | 5 |
C | 5 | 5/2/2023 | 109 | 9 |
C | 6 | 5/3/2022 | 293 | 12 |
I have created a calendar table , which has all dates form 2019 to 2024. This date from calendar table is in one to many relationship with Sale Date
Following is what is the desired result in power bi
2023 | |||||
State | Current year Sale | Carry forward Sale | |||
A | 229.8462 | 63.4 | |||
B | |||||
C |
this is a simple presentation , i have only calculated value for 2023 , we do have to include months and other years
Defination :
Current year Sale - To calculate this first we need sale per month i.e Sales / Service Period
Important thing to keep in mind that current year sale means addition of sales/service period for the year it was sold (Sale date)
for example if sale date is 01/01/2022 then for that specific sale ID current year sale for all years will be 0 except 2022
example from dummy data for first row sale/service period = 19.15
this for service period of 13 months
but as the current year only can be for 12 months hence left out months after sale date is jan(As sale was done in jan) - dec = 12
hence 19.15*12 = 229.8
And as ID 2 was not sold in 2023 , hence final Current year sale for A in 2023 will be 229.8+0 = 229.8
and the rest left will go for carry forward sales for the same saleID for next year. But remeber we only calculate carry forward for 2 years or left our service period after current year sale (whichever smaller)
Like for example from dummy data for ID 2 , it was sold in 2022 , hence that means its carry forward will be accounted in 2023 ,
hence for ID 2 ,
Sale/sercvice period = 317/15 = 21.13
for its carry foward period lets first find out current year period of same ID
for ID 2 service period is 15 months and it was sold in january hence leaving (jan-dec) 12 months rest is carry forward period of 3 months (here we used 3 as 3 months is smaller than 2 years, as mentioned above that for carry forward only take left out servic period after current year sale or 2 years whichever smaller )
hence 3*21.13 = 63.4
as there is no other ID which was sold in 2021 or 2022 for A
Hence carry forward for A is 63.4
Please help
@dax
@GuyInACube
@amitchandak
@AbbasG
@technolog
Solved! Go to Solution.
Hi @Anonymous ,
Please try:
Carry forward Sale2 =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SELECTCOLUMNS ( 'Table', "State", [State] )
var _e = SELECTCOLUMNS('Table',"Date",[Sale Date])
VAR _d =
SUMX (
FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a &&[Sale Date] in _e),
SWITCH (
TRUE (),
NOT([Sale Date] in SELECTCOLUMNS('Table',"Date",[Sale Date])),0,
DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period (in months)],
DIVIDE ( [Total Sale], [Service Period (in months)] )
* ( [Service Period (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) ),
0
)
)
RETURN
_d
Final output:
Best Regards,
Jianbo Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
I made some changes to and this works perfectly
Carry forward Avoidance =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SELECTCOLUMNS ( 'Data', "Catg", [Catg] )
var _e = SELECTCOLUMNS('Data',"Date",[SaleDate])
VAR _d =
SUMX (
FILTER ( ALL ( 'Data' ), [Catg] IN _c && [SaleDate] <= _a && Data[SaleDate] in _e ),
SWITCH (
TRUE (),
NOT([Saledate] in SELECTCOLUMNS('Data',"Date",[Saledate])),0,
DATEDIFF ( [Saledate], _a, YEAR ) = 1,
SWITCH (
TRUE (),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]
&&
Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) <= 12
,
Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]
&&
Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) >12
,
12,
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) + 12 < Data[ServiceMonths] , 12 ,
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) >= Data[ServiceMonths] , 0 ,0)
,
DATEDIFF ( [Saledate], _a, YEAR ) = 2,
SWITCH (
TRUE (),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 <= Data[ServiceMonths]
, 12,
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 >= Data[ServiceMonths]
, Data[ServiceMonths]-((DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 ),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)>=Data[ServiceMonths],0,0),0)
* DIVIDE(Data[AvoidanceSale],Data[ServiceMonths]))
return _d
Current year Avoidance sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
SUMX (
FILTER('Contracts',Contracts[Saledate]<=_a&&Contracts[Saledate]>=MINX('Calendar',[Date])),
DIVIDE ( Contracts[AvoidanceSale], [Service Months] )
* (
MINX (
{ DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },
[Value]
)
+ IF (
MINX (
{ DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },
[Value]
) = [Service Months],
0,
1
)
)
)
RETURN
_b
I'm wondering if anyone can help me with a MEASURE calculation within a tabular report in Power BI desktop.
Here is the challenge. I have a tabular visual in PBI desktop producing trip data including Vehicle Registration, Start date and time and End date and time from some different tables in my sql database. I also have a final end odometer reading but I do not have the start odometer reading in each row of data. I need a MEASURE formula to lookup the previous date and time based on the Vehicle Registration so that i have a Start and End Odometer reading and then from that I calcxulate the journey length. Is this possible?
The Vehicle table stores the Registration and another table stores the dates / times and the end odometer reading. The two tables are connected by a Vehicle Id.
I made some changes to and this works perfectly
Carry forward Avoidance =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SELECTCOLUMNS ( 'Data', "Catg", [Catg] )
var _e = SELECTCOLUMNS('Data',"Date",[SaleDate])
VAR _d =
SUMX (
FILTER ( ALL ( 'Data' ), [Catg] IN _c && [SaleDate] <= _a && Data[SaleDate] in _e ),
SWITCH (
TRUE (),
NOT([Saledate] in SELECTCOLUMNS('Data',"Date",[Saledate])),0,
DATEDIFF ( [Saledate], _a, YEAR ) = 1,
SWITCH (
TRUE (),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]
&&
Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) <= 12
,
Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) + 12 >= Data[ServiceMonths]
&&
Data[ServiceMonths] - (DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1 ) >12
,
12,
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) + 12 < Data[ServiceMonths] , 12 ,
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1) >= Data[ServiceMonths] , 0 ,0)
,
DATEDIFF ( [Saledate], _a, YEAR ) = 2,
SWITCH (
TRUE (),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 <= Data[ServiceMonths]
, 12,
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 < Data[ServiceMonths]
&&
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 + 12 >= Data[ServiceMonths]
, Data[ServiceMonths]-((DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)+12 ),
(DATEDIFF(Data[Saledate],DATE(YEAR(Data[Saledate]),12,31),MONTH)+1)>=Data[ServiceMonths],0,0),0)
* DIVIDE(Data[AvoidanceSale],Data[ServiceMonths]))
return _d
Current year Avoidance sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
SUMX (
FILTER('Contracts',Contracts[Saledate]<=_a&&Contracts[Saledate]>=MINX('Calendar',[Date])),
DIVIDE ( Contracts[AvoidanceSale], [Service Months] )
* (
MINX (
{ DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },
[Value]
)
+ IF (
MINX (
{ DATEDIFF ( 'Contracts'[Saledate], _a, MONTH ), [Service Months] },
[Value]
) = [Service Months],
0,
1
)
)
)
RETURN
_b
Hi @Anonymous ,
Please try:
Carry forward Sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SELECTCOLUMNS ( 'Table', "State", [State] )
VAR _d =
SUMX (
FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a ),
SWITCH (
TRUE (),
DATEDIFF ( [Sale Date], _a, YEAR ) = 1,
SWITCH (
TRUE (),
[Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 > 12, 12,
[Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 <= 12
&& [Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 > 0,
[Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1,
0
),
DATEDIFF ( [Sale Date], _a, YEAR ) = 2,
SWITCH (
TRUE (),
[Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ) + 1, 12, 31 ), MONTH ) - 1 >= 12, 12,
[Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ) + 1, 12, 31 ), MONTH ) - 1 < 12
&& [Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ), 12, 31 ), MONTH ) - 1 > 0,
[Service Period (in months)]
- DATEDIFF ( [Sale Date], DATE ( YEAR ( [Sale Date] ) + 1, 12, 31 ), MONTH ) - 1,
0
),
0
)
* DIVIDE ( [Total Sale], [Service Period (in months)] )
)
RETURN
_d
Final output:
Best Regards,
Jianbo Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thanks @v-jianboli-msft This is wonderful
But i still have one last problem filtering
When i filter using Sale date , still the carry forward doesnt change it keeps calculating irresepective of filter
What should i change in the measure of carry forward which will make me use sale date as filter and help me calculate carry forward of only Sale ID's which were sold in selected dates in the filter
Please help @v-jianboli-msft this is the last thing that is missing that is filtering using sale date for both carry forward and current year sale
other than that the answers are correct please after this I will accept the solution
I really appreciate your help @v-jianboli-msft
Please help
Hi @Anonymous ,
Please try:
Carry forward Sale2 =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SELECTCOLUMNS ( 'Table', "State", [State] )
var _e = SELECTCOLUMNS('Table',"Date",[Sale Date])
VAR _d =
SUMX (
FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a &&[Sale Date] in _e),
SWITCH (
TRUE (),
NOT([Sale Date] in SELECTCOLUMNS('Table',"Date",[Sale Date])),0,
DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period (in months)],
DIVIDE ( [Total Sale], [Service Period (in months)] )
* ( [Service Period (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) ),
0
)
)
RETURN
_d
Final output:
Best Regards,
Jianbo Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @Anonymous ,
Please try:
Remove the relationship between Calendar and Table
Then apply the measrue:
Carry forward Sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SELECTCOLUMNS ( 'Table', "State", [State] )
VAR _d =
SUMX (
FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a ),
SWITCH (
TRUE (),
DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period (in months)],
DIVIDE ( [Total Sale], [Service Period (in months)] )
* ( [Service Period (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) ),
0
)
)
RETURN
_d
Current year Sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
SUMX (
FILTER('Table',[Sale Date]<=_a&&[Sale Date]>=MINX('Calendar',[Date])),
DIVIDE ( [Total Sale], [Service Period (in months)] )
* (
MINX (
{ DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period (in months)] },
[Value]
)
+ IF (
MINX (
{ DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period (in months)] },
[Value]
) = [Service Period (in months)],
0,
1
)
)
)
RETURN
_b
Final output:
Best Regards,
Jianbo Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hello @v-jianboli-msft ,
I have summarized the problem here
please give it a last try I beg
Here is the power bi visual (with measures that you helped with and raw data )
Here is the correct answer which i calculated manually for same data
Here is the explanation of the formula i used in excle and some notes
Here is the google drive link for same excle file i used
https://drive.google.com/file/d/11sH1i9ut0whz6_XMqgTRbecGhK8uF7oS/view?usp=sharing
Hi @Anonymous ,
Please try:
Current year Sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
SUMX (
'Table',
DIVIDE ( [Total Sale], [Service Period (in months)] )
* (
MINX (
{ DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period (in months)] },
[Value]
) + 1
)
)
RETURN
_b
Carry forward Sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SUMX (
FILTER (
ALL ( 'Table' ),
[State] = SELECTEDVALUE ( 'Table'[State] )
&& [Sale Date] <= _a
),
SWITCH (
TRUE (),
DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period (in months)],
DIVIDE ( [Total Sale], [Service Period (in months)] )
* ( [Service Period (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) )
)
)
RETURN
_c
Final output:
Best Regards,
Jianbo Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hello @v-jianboli-msft
I have tried manual calculation to see which measure is wrong
This is measure that you helped me with
This is manual calculation (these numbers should match)
for B state
2020 - Current Year - 174.6
2020 - Carry forward - 0
2021 - Current Year -388
2021 - Carry Forward - 116.4
Hence when I can see Current Year sale is wrong as number for 2021 doesnt match for B when tried with your measure and manually in excel
@v-jianboli-msft
I changed your measure of current year sale to followinf
Current year Sale =
VAR _a = MAXX ( 'Calendar', [Date] )
VAR _b = SUMX (
'Table',
DIVIDE ( [Total Sale], [Service Period (in months)] )
* (
MINX (
{ DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period (in months)] },
[Value]
) + IF(MINX (
{ DATEDIFF ( 'Table'[Sale Date], _a, MONTH ), [Service Period (in months)] },
[Value]
) = [Service Period (in months)], 0, 1)
)
)
RETURN _b
And now everything is perfect
One last help with same measure
Why is column level total not working for carry forward 😞
Hi @Anonymous ,
Please try:
Carry forward Sale =
VAR _a =
MAXX ( 'Calendar', [Date] )
VAR _b =
MINX ( 'Calendar', [Date] )
VAR _c =
SELECTCOLUMNS ( 'Table', "State", [State] )
VAR _d =
SUMX (
FILTER ( ALL ( 'Table' ), [State] IN _c && [Sale Date] <= _a ),
SWITCH (
TRUE (),
DATEDIFF ( [Sale Date], _a, YEAR ) >= 2, 0,
DATEDIFF ( [Sale Date], _a, YEAR ) <= 0, 0,
DATEDIFF ( [Sale Date], _b, MONTH ) <= [Service Period (in months)],
DIVIDE ( [Total Sale], [Service Period (in months)] )
* ( [Service Period (in months)] - DATEDIFF ( [Sale Date], _b, MONTH ) ),
0
)
)
RETURN
_d
Final output:
Best Regards,
Jianbo Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hello @v-jianboli-msft
Thank you so much
The Current year sale should stop at the year of last sale date (in my actual official data its 2023, hence current year for 2024 and 2025 will not show up )
BUt carry forward sale should show maximum upto 2 years
As carry forward is for years after the year it was sold , but using your carry forward sale measure , it stops calculating on year 2023 , please can you help so that it can go upto two years or service period which ever is minimum for that sale ID
I really appreciate your help
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
113 | |
109 | |
100 | |
39 | |
31 |