Forum Discussion
Need help dax measure
- 1 year ago
Hi tkavitha911 have done this using m code, please take a look. Here the daily container capacity is set to 2 for example
In the advanced editor, add this
#"Added Daysused" = Table.AddColumn(#"Changed Type", "Daysused", each Duration.Days([PredictedDelivery] - [ETA])),
#"Added Overdue Flag" = Table.AddColumn(#"Added Daysused", "Overdue flag", each [Daysused] > [FreeDays]),
DailyCounts = Table.Group(#"Added Overdue Flag", {"PredictedDelivery"}, {
{"ContainerList", each _, type table [
Plant=nullable text,
Container Number=nullable text,
ETA=nullable date,
PredictedDelivery=nullable date,
FreeDays=nullable number,
Daysused=nullable number,
Overdue flag=nullable logical
]},
{"DailyCount", each Table.RowCount(_), Int64.Type}
}),Sorted = Table.Sort(DailyCounts, {"PredictedDelivery", Order.Ascending}),
DailyCapacity = 2,
RowCount = Table.RowCount(Sorted),ResultList = List.Generate(
() => [i = 0, carry = 0, list = {}],
each [i] < RowCount,
each [
row = Sorted{i},
date = row[PredictedDelivery],
count = row[DailyCount],
containerList = row[ContainerList],
totalToday = count + [carry],
processed = if totalToday <= DailyCapacity then totalToday else DailyCapacity,
newCarry = if totalToday > DailyCapacity then totalToday - DailyCapacity else 0,
newRow = [
PredictedDelivery = date,
ContainerList = containerList,
DailyCount = count,
AdjustedCount = processed,
CarryForward = newCarry
],
list = [list] & {newRow},
i = [i] + 1,
carry = newCarry
]
),CarryForwardTable = Table.FromRecords(ResultList{List.Count(ResultList)-1}[list])
in
CarryForwardTable - Anonymous1 year ago
Hi tkavitha911,
To calculate whether the predicted delivery is delayed beyond the allowed free days, you can create a calculated column (or a measure, depending on your visual/reporting needs) using the following DAX:
Delay Beyond Free Days =
VAR DelayDays = DATEDIFF('Table'[Max of ETA], 'Table'[Predicted_Delivery_Date], DAY)
RETURN
IF(DelayDays > 'Table'[FT DAYS], DelayDays, BLANK())For handling overflow based on daily capacity part, we’d need to simulate the logic where containers are processed based on a daily capacity limit, and any overflow is carried over to the next day.
This kind of logic is difficult to implement with DAX alone, since DAX is not well-suited for row-wise iterative logic with carryovers. Instead, I recommend using Power Query (M language) for this step.
Here’s a high-level outline of how you could implement it in Power Query:
-
Group containers by date (Predicted Delivery Date).
-
Sort dates in ascending order.
-
Define a parameter for daily capacity (e.g., 5 containers per day).
-
Use a loop or custom column logic to:
-
Check if the container count on a given date exceeds the capacity.
-
If it does, carry the excess forward and add it to the next date’s count.
-
Continue this iteratively for all dates.
-
This might involve writing a custom function or using a running total approach to track the overflow.
Best Regards,
Hammad.
-
Hi tkavitha911,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution so that other community members can find it easily.
Thank you.
Hi tkavitha911,
May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.
Thank you.