Running total based on other columns value - powerbi

I need to calculate running total of forecast amount starting from maximum value, based on two stock locations and once for each product (product numbers are repetitive due to stock locations). e.g product no "1" should be used once for running total.
My first code, which didn't sum the same forecast amounts independently based on item no.
RunningTotal1 =
VAR
CurrentAmount= Listing[Forecast Amount]
RETURN
SUMX (
FILTER (
Listing;
Listing[Forecast Amount]>= CurrentAmount);
Listing[Forecast Amount])
My second code where running total was based on stock location, it calculates for each location independently still didn't sum the same forecast amounts independently based on item no.
RunningTotal2 =
VAR CurrentAmount = Listing[Forecast Amount]
VAR Location = Listing[Stock Location]
RETURN
SUMX (
FILTER (
Listing;
Listing[Stock Location] = Location &&
Listing[Forecast Amount]>= CurrentAmount);
Listing[Forecast Amount])
But when I add second location to my formula it gives an error.
"DAX comparison operations do not support comparing values of type Text with values of type True/False. Consider using the VALUE or FORMAT function to convert one of the values."
RunningTotal3 =
VAR CurrentAmount = Listing[Forecast Amount]
VAR LocationW = Listing[Stock Location] = "Warehouse"
VAR LocationT = Listing[Stock Location] = "Total Stock"
RETURN
SUMX (
FILTER (
Listing;
Listing[Stock Location] = LocationW ||
Listing[Stock Location] = LocationT &&
Listing[Forecast Amount]>= CurrentAmount);
Listing[Forecast Amount])
What I expect is
Hello #RADO,
I tried to add as measure but couldn't succeed therefore I added as a new column. I realize my code is wrong, in your image item no 5 and 6 have different number. my formula is
Forecast Index = RANKX(Listing;Listing[Forecast Amount])
here's the result Forecast Index
regards

You need to add a column to your "Listing" table that defines order for the running total. You can't use Forecast Amount for that, because there are cases where different items have the same amounts (for example, items 66 and 99), and there is no way to resolve these ties (which items should be accumulated first - 66 or 99? No way to tell).
Often, date/time fields are used for that, but if you don't have them, you can add an index based on whatever rules you need. For this example, I manually added "Forecast Index" as follows:
Forecast index here is simply a sorting order - if you sort by it, you will get the layout exactly matching your "desired result" table.
Then, create a measure:
RT Expected =
VAR Current_Index = MAX ( Listing[Forecast Index] )
VAR Summary_Table =
SUMMARIZE (
FILTER (
ALL ( Listing ),
Listing[Forecast Index] <= Current_Index &&
Listing[Stock Location] <> "Without Stock" ),
Listing[Item No],
Listing[Forecast Amount] )
RETURN
SUMX ( Summary_Table, Listing[Forecast Amount] )
Result:
Note: If you don't want to see Items 2 and 4, simply remove them from the visual filter.
How it works:
First, we use FILTER to create a virtual table that a) ignores "Without Stock" locations, and b) keeps only forecast amounts that we need for the running total;
Second, we use SUMMARIZE to group that virtual table by Item No and Forecast Amount. The grouping eliminates duplicates;
Finally, we use SUMX to iterate the de-duplicated table, and sum up all relevant amounts.
Edit:
You can create a proper index using PowerQuery:
In your left panal, Go to "Data", and select "Listing" table;
Right-click the table, and select "Edit Query". Power BI will take you to the Power Query window;
In Power Query, first, sort column "Forecast Amount" Descending. Then, sort column "Item No" Ascending. This will arrange your records in the same order as you have them in your picture;
Finally, in Power Query window, go to "Add Column", then "Index Column", select "From 1". Power Query will create an index column for you, and name it "Index". You can rename it as needed (i.e, to "Forecast Index");
Click "Close and Apply", and then build the measure I suggested using this new index column as an input.
In case these instructions are not clear, refer to this article:
https://www.excelguru.ca/blog/2018/06/14/ranking-method-choices-in-power-query/

Related

calculated column stops working when exdternal filter used

I have some Fact Revenue, I am trying to group by Conca, and display the values only if negative…
For doing it I have this calculated column:
=
VAR name1 = Revenue[Conca]
VAR name2=
CALCULATE (
SUM ( Revenue[CostOfQuality] ),
FILTER ( Revenue, Revenue[Conca] = name1 )
)
RETURN
if (name2>0, 0, Revenue[CostOfQuality])
It works:
(highest value is 0 as expected):
Now...
If I drag fiscal year it stops working:
Why is it that I see numbers higher than 0?? (I want it to still work even if I bring other filters...)
a calculated column is computed computed once on the whole dataset after the data are loaded, and is basically a "static column".
Whatever the expected result, you are looking for a measure, which gets computed in the current context
Calculated columns and calculated tables are different from measures, since they are static: You can filter them, but they don't recalculate on filter changes.

COUNTAX function will not return output if I set the filter criteria to a calculation within the Power BI workbook

I am trying to create a measure that will display the number of times a specific price occurs within the price column. The specific price comes from a measure that is calculating the optimal price. When I set the filter equal to the optimal price calculation, it just returns blank.
In this particular example, the measure that gives me the optimal price is saying that the optimal price should be $1.00. So if I manually input 1.00 into the filter section, my output comes back correctly. Why doesn't this work when I set my filter to the optimal price calculation which is the exact same value as me manually entering 1.00?
Again, this calculation works just fine if I change the price filter to Table_Name'[Price] = 1 instead of putting it equal to the Optimal Price Calculation.
Optimal_Price_Count =
COUNTAX (
FILTER (
'Table_Name',
'Table_Name'[Price] = [Optimal Price Calculation]
&& 'Table_Name'[Product] = SELECTEDVALUE ( 'First Product'[Product] )
),
'Table_Name'[Price]
)
The [Optimal Price Calculation] measure is calculated within the FILTER function, which means there is a context transition that turns row context into filter context for the measure.
I'd recommend defining this as a variable and then referencing it. You can do the same for the product too if you like:
Optimal_Price_Count =
VAR OptimalPrice = [Optimal Price Calculation]
VAR SelectedProduct = SELECTEDVALUE ( 'First Product'[Product] )
RETURN
COUNTAX (
FILTER (
'Table_Name',
'Table_Name'[Price] = OptimalPrice
&& 'Table_Name'[Product] = SelectedProduct
),
'Table_Name'[Price]
)

Set First day of the range as Initial Inventory + Production - Sales = Inventory (DAX)

My challenge has been how to
01 - set Initial Inventory (considering the first day of the column);
02 - From the second day on, have Forecast (Previsão) - Sales (Vendas) = Balance. So 3 columns for each day
The way it is now, it repeats Initial Inventory throughout the days.
Here's the goal I'm trying to achieve:
Here's how the tables are related:
I would try to approach it with an unrelated helper table, that would contain 2 columns and 4 rows. Here's an example based on Contoso data.
Here's the helper table. Column Item ID is used for sorting result columns as well:
And now comes the magic:
Helper measure =
VAR minDate =
CALCULATE ( MIN ( DimDate[Datekey] ), ALLSELECTED ( DimDate ) )
VAR curDate =
SELECTEDVALUE ( DimDate[Datekey] )
RETURN
SWITCH (
SELECTEDVALUE ( Helper[Item ID] ),
1, IF ( curDate = minDate, [Opening Inventory Qty], BLANK () ),
2, [Production Qty],
3, - [Sales Qty],
4, [Closing Inventory Qty]
)
Here's an explanation:
minDate is used to get initial date on the visual
curDate is used to get current date
Next I check, which column from the helper table the measure is calculated for using SWITCH function
using SWITCH, I return different formula, based on helper column type
for Opening inventory, I only return a value when current date is equal to initial date. Otherwise, I return BLANK() - that's why Opening inventory only appears on the first date
The matrix visual is built using Product name in rows; Year, month, day and Item (from the helper table) in columns, and the Helper measure from above.
Here's a the result (don't mind quantities, it's a sample data):
EDIT:
For completeness' sake, here's how the model looks. Notice that the Helper table is not related to any other table:

Filter existing table to another table without adding measures or column on existing table

I want to create a table based on input table.
Input table is:
The new table filters the input table to show the last entry of every day.
I have tried working with measure but sometimes cant tell if it is working right until I graph it in pivot tables which is not so bad but sometimes just doesn't show me what I need to see exactly.
I have tried this measure:
History_Daily Efficiency =
VAR LastDailyEfficiency =
GENERATE(
VALUES ('Table_Full'[Cell]),
CALCULATETABLE (
TOPN (
1,
GROUPBY (
'Table_Full',
'Table_Full'[Date],
'Table_Full'[Time],
'Table_Full'[Efficiency]
),
'Table_Full'[Date], DESC,
'Table_Full'[Time], DESC,
'Table_Full'[Efficiency], ASC
)
)
)
RETURN
CALCULATE (
AVERAGE('Table_Full'[Efficiency]),
TREATAS( LastDailyEfficiency, 'Table_Full'[Cell], 'Table_Full'[Date], 'Table_Full'[Time], 'Table_Full'[Efficiency]),
'Table_Full'[Efficiency] < 80
)
But I got this:
I would like to see this as the output:
You can create a new table:
LastDayCount = GROUPBY(Table_Full;Table_Full[lob/Part Number];Table_Full[Date];"LastDate";MAXX(CURRENTGROUP(); Table_Full[DateTime]))
This will create a table with the last DateTime of the day.
Next we add a column giving us the max of that particular last datetime of the day. I noticed that you have more the same entries, the logic below takes the max part count at the end of the day when more than one entry.
Count =
CALCULATE(MAX(Table_Full[Part Count]);
FILTER(Table_Full;LastDayCount[Table_Full_lob/Part Number] = Table_Full[lob/Part Number]
&& LastDayCount[LastDate] = Table_Full[DateTime]))
End result:

Including groups with no values in stacked column chart

I'm building a chart that will cumulatively sum Invoice Values for the next month, broken out by category of sale. It looks like this:
My problem is that for particular slicer values, there might not be any invoices for a particular category, and thus the groups just don't show in the graph:
This looks scrappy, so I'm trying to force them to show. Given the rows simply don't exist the way I'm trying to do this is to have a new table which has a row per-date-per-category, and use a measure to cumulatively sum all the data from my source table. So for example given this source table:
I've built the structure of this table, but I need to find a way to add the "Cumulative Value" field that's also shown:
Unfortunately I can't work out how to make that work. The usual cumulative sum syntax would be:
Cumulative Value = (
CALCULATE(
SUM('Table 1'[Value]),
FILTER(ALLSELECTED('Table 1'), ISONORAFTER('Table 1'[Date], MAX('Table 1'[Date]), DESC))
)
)
And I can't seem to add in another filter expression without either
Breaking it such that it returns different values per category but the same value for every date
Breaking it such that it returns different values per date but the same value for each category
So; what Measure can I build to create that "Cumulative Value" field?
Never mind, I got it. Full DAX for the answer was:
CumulativeValue =
VAR CurrActionDate = MAX('Table 2'[Date])
VAR CurrTransType = MAX('Table 2'[Category])
RETURN (
CALCULATE(
SUM('Table 1'[Value],
FILTER(
ALLSELECTED('Table 1'),
'Table 1'[Date] <= CurrActionDate
),
FILTER(
ALLSELECTED('Table 1'),
'Table 1'[Category] = CurrTransType
)
)
)
Ta-da! Cumulative sum across different groups with no blank values.