Forum Discussion
Need urgent help - DAX calculation
Hi people,
My question is still not solved yet. Can anyone please help?
@parry2k @mahoney19 @Amit @parry2k @az38 @jdbuchanan71 @mahoneypat @edhans @harshnathani @v-kellya-msft @MFelix @Ashish_Mathur @BA_Pete @ryan_mayu @kbuckvol @Alexander76877 @Petazo @Mariusz @TomMartens @Greg_Deckler @tjd @Sean @mikstra @AllisonKennedy @EricHulshof @briandpeterson @USG_Phil @vpatel55 @mwegener @v-piga-msft @tex628 @sturlaws @Vvelarde @CheenuSing @MarcelBeug @Zubair_Muhammad @v-piga-msft @danextian @MattAL @MattAllington @roalexan @Alexander76877 @kgc
Hi Kolumam ,
as the others have mentioned already, the problem with the sample you've provided is that there are inconsistencies/errors in it that make determine the desired logic hard.
The following code calculates proportional values according to the actual number of days in the year:
let
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText(
"dc1LCsAwCATQu7gOqPkQc5OW4P2vUQ2RhkJ3M/IY5wTGjpl4QILCKJEv772BJifjIMVypk0qbZKxxd2MVSscOxI71Xf6u8Ni+V6ETiKfV04qb8I/r9YOFVB9AA==",
BinaryEncoding.Base64
),
Compression.Deflate
)
),
let
_t = ((type nullable text) meta [Serialized.Text = true])
in
type table[
#"Start Date of Contract" = _t,
#"End Date of Contract" = _t,
#"Operation Name" = _t,
#"Comprehensive O&M Price" = _t
]
),
#"Changed Type" = Table.TransformColumnTypes(
Source,
{
{"Start Date of Contract", type date},
{"End Date of Contract", type date},
{"Operation Name", type text},
{"Comprehensive O&M Price", Int64.Type}
}
),
AddListOfYears = Table.AddColumn(
#"Changed Type",
"Year",
each {Date.Year([Start Date of Contract])..Date.Year([End Date of Contract])}
),
#"Expanded ListOfYears" = Table.ExpandListColumn(AddListOfYears, "Year"),
AddStart = Table.AddColumn(
#"Expanded ListOfYears",
"Start",
each List.Max({[Start Date of Contract], #date([Year], 1, 1)})
),
AddEnd = Table.AddColumn(
AddStart,
"End",
each List.Min({[End Date of Contract], #date([Year], 12, 31)})
),
AddDuration = Table.AddColumn(
AddEnd,
"DurationInDays",
each Duration.Days([End] - [Start]) + 1,
Int64.Type
),
AddDaysInYear = Table.AddColumn(
AddDuration,
"DaysInYear",
each if Date.IsLeapYear(#date([Year], 1, 1)) then 366 else 365
),
AddAnnualShare = Table.AddColumn(
AddDaysInYear,
"AnnualShare",
each [DurationInDays] / [DaysInYear]
),
WeightPriceByShare = Table.AddColumn(
AddAnnualShare,
"AnnualContractValue",
each [#"Comprehensive O&M Price"] * [AnnualShare],
type number
)
in
WeightPriceByShare
It creates a table in the query editor that will hopefully allow you to follow the logic and see where your numbers are wrong:
In general your numbers are too low because your using 365 days a year and weight your months only with 30 days (adding up to 360).
A specific inconsistency in your calculation is that you allocate partial end years pro rata (i.e. Y 410 for 2020: 90 days (Jan-Mar)) but all partial start years with full year (Y 380 with 365 days, although the contract starts in April and X 395 divided by only 180 (instead of 360)).
So my calculation assumes that the "Comprehensive O&M Price" is an annual fee that has to be allocated to partial years consistently.