How to get Closing Inventory Qty right - DAX - matrix

I can't find the right results for Closing Inventory, given the scenario below:
Measure added to Matrix Table:
Calculation=
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
RETURN
SWITCH (
SELECTEDVALUE ( HelperTable[ID] ),
1, IF ( curDate = minDate, [OpenInventoryMeasure], BLANK () ),
2, IF ( curDate > minDate, [ProdMeasure], BLANK () ),
3, [QtySalesMeasure],
4, if (curDate > minDate,[Closing Inventory], BLANK())
)
Here are the measures created and used in the main calculation above:
OpenInventoryMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
return
CALCULATE(Sum('Production'[Qty]),FILTER('Calendar','Calendar'[Date] = minDate))
ProdMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
VAR curDate =
SELECTEDVALUE ( 'Calendar'[Date] )
return
CALCULATE(Sum('Production'[Value]),FILTER('Calendar','Calendar'[Date] > minDate))
QtySalesMeasure = SUM(PedItens[QTDUND])
ClosingMeasure = [OpenInventoryMeasure ] + [ProdMeasure] - [QtySalesMeasure]
Main issue: Right now, Closing measure is not giving me the right result, although the measure that make it up are correct.
Tiny issue: Also, the order is not right in the table, although I had ordered it by ID.
Here's a screenshot of the resulting table:
I appreciate any help.

The problem is with the Opening Inventory measure, because when you do the CALCULATE you are now on the context of the Monday, so it's already filtered by Monday and then you're adding an additional filter of date = 7th which returns blank.
You need to clear the filters first and then apply the date filter:
OpenInventoryMeasure =
VAR minDate =
CALCULATE ( MIN ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar' ) )
return
CALCULATE(Sum('Production'[Qty]), ALLSELECTED('Calendar'), 'Calendar'[Date] = minDate)
Your Prod measure probably suffers from the same error, but I don't understand what's the logic behind it because you only filter based on min date and not on current date.

Related

Grand Total is incorrect is SSAS Tabular Model

The number I see in the grand total is different from the sum of the rows (for previous year column [Önceki Yıl IPP]).
Table columns
Measures:
IPP Ciro:=SUM([Amount_])/1000
This measure calculates sum of revenue(Amount_)
Önceki Yıl IPP:=
VAR LastDaySelectionIPP =
LASTNONBLANK ( Tarih[Tarih], [IPP Ciro] )
VAR CurrentRangeIPP =
DATESBETWEEN ( Tarih[Tarih], MIN ( Tarih[Tarih] ), LastDaySelectionIPP )
VAR PreviousRange =
SAMEPERIODLASTYEAR ( CurrentRangeIPP )
RETURN
IF (
LastDaySelectionIPP >= MIN ( Tarih[Tarih] ),
CALCULATE ( [IPP Ciro], PreviousRange )
)
This measure calculates sum of previous year revenue.
I want to compare the current year with the last year same period.
Why is the Grand total is 98.998 for previous year[Önceki Yıl IPP]?
Grand Total is incorrect is SSAS Tabular Model
It’s not jumping out at me, but here’s how to debug it.
Add a new measure like this and add it to your table:
Debug:=
VAR LastDaySelectionIPP =
LASTNONBLANK ( Tarih[Tarih], [IPP Ciro] )
VAR MinDate = MIN ( Tarih[Tarih] )
VAR CurrentRangeIPP =
DATESBETWEEN ( Tarih[Tarih], MIN ( Tarih[Tarih] ), LastDaySelectionIPP )
VAR PreviousRange =
SAMEPERIODLASTYEAR ( CurrentRangeIPP )
VAR WrongAnswer =
IF (
LastDaySelectionIPP >= MIN ( Tarih[Tarih] ),
CALCULATE ( [IPP Ciro], PreviousRange )
)
RETURN LastDaySelectionIPP & “,” & MinDate & “,” & [IPP Ciro] & “,” & WrongAnswer
It will list the values you are using in the calculation and I predict you’ll find the unexpected value. Update us on what you find!

How to count two column values

I want count two columns using power bi to create a visual.my measure as below
Test Measure = COUNTA('Export'[Line])+COUNTA('Export'[Line 2]),
You need to create a calculated table with all the lines values, like this
TableValues =
DISTINCT ( UNION ( DISTINCT ( 'Table'[Line] ), DISTINCT ( 'Table'[Line_2] ) ) )
With that table done, you can write the following measure
CountValues =
VAR _CurrentValue =
SELECTEDVALUE ( TableValues[Line] )
VAR _C1 =
CALCULATE ( COUNTA ( 'Table'[Line] ), 'Table'[Line] = _CurrentValue )
VAR _C2 =
CALCULATE ( COUNTA ( 'Table'[Line_2] ), 'Table'[Line_2] = _CurrentValue )
RETURN
_C1 + _C2
The calculations count all the instances of a specific Line. It's important that the TableValues doesn't have any relationship with the other tables.
Output
Using 'TableValues'[Line] as the Line and CountValues as the metric.

Problem in using filter context in CALCULATE

In PowerPivot DataModel I have written following DAX Code:
=VAR var1 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
ALL ( 'CapitalPrepayment'[EffectiveYear] )
)
VAR var2 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[EffectiveYear] = "1399",
'CapitalPrepayment'[Subject] = "Invoice"
)
VAR var3 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
'CapitalPrepayment'[EffectiveYear] = "1398"
)
VAR var5 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
ALL ( 'CapitalPrepayment'[Subject] )
)
RETURN
IF ( Var5 < 0, 0, IF ( Var5 = 0, 0, IF ( VAR1 + VAR2 <= 0, Var3, 0 ) ) )
and the result is:
As you can see, the problem is that grand total of [Var5] for some vendors and grand total for [Final] for all vendors is highly related to filters come from EffectiveYear ( at least Ithink so ), so the result would be zero. in addition, changing the filter context from visual ( for example removing [vendor] and [EffectiveYear] ) will disrupt the result. how can I fix this?
In my written DAX, [Var1]~[Var3] do include [subject] and [EffectiveYear] as filter arguments, but [Var5] does not for [EffectiveYear] . so I expect that the problem arise from [EffectiveYear].
I tried to include [EfectiveYear] in someway in CALCULATE but it didn't work.

Function 'SAMEPERIODLASTYEAR' expects a contiguous selection Issue

I have a tabular data model in Visual Studio, I made a formula to get last year's sales over a given period:
Selected Measure LY:= CALCULATE([Selected Measure], SAMEPERIODLASTYEAR('date' [date]))
I made a formula to have the decomposition of all the months in the 'date' table:
FY_Month='date' [FY Month No]&'date' [Month_label]
And:
FY_Month_No=format(if('date'[month_no]>6,'date'[month_no]-6,'date'[month_no]+6), "00")
Month_label : all the months of the year
The "Selected Measure LY" display works fine when I have all months selected but when I remove one I get this error message:
Calculation error in "Selected Measure LY": the "SAMEPERIODLASTYEAR" function expects a contiguous selection when the date column comes from a tabkle on side 1 of a bidirectional relationship.
Between my 'date' table and my sales table I have a 1/N relationship, I tried to change this relationship too but the error always comes back.
I looked for solutions on forums and I found this but I don't find the same numbers as with my first formula:
Selected Measure LY:=
CALCULATE([Selected Measure],
FILTER (
ALL ( 'date' ),
YEAR ( 'date'[date] ) = YEAR ( TODAY () )
&& 'date'[date] <= TODAY ()
)
)
My code for Selected Measure:
Selected Measure:=
VAR hasFilter =
HASONEFILTER ( 'Measure Selection'[Measure Name] )
VAR selMeas =
SELECTEDVALUE ( 'Measure Selection'[Measure Name], BLANK () ) /*SWITCH works slow, so using many IFs*/
VAR preCalc =
IF (
selMeas = "Units",
[Sku Piece Quantity],
IF (
selMeas = "Net Sales",
[Euro Net Amount],
IF (
selMeas = "Gross Sales",
[Euro Gross Amount],
IF (
selMeas = "Average Wholesale",
[Average_Wholesale],
IF (
selMeas = "Average Units",
[Average Units],
IF (
selMeas = "No of Styles",
[Style Count],
IF (
selMeas = "No of Colours",
[Colour Count],
IF (
selMeas = "ROD by Style",
[ROD by Style],
IF (
selMeas = "Standard Cost",
[Sum_Cost_Base],
IF (
selMeas = "Net Margin",
[Net_Margin],
IF (
selMeas = "Gross Margin",
[Gross_Margin],
IF ( selMeas = "ROD by Colour", [ROD by Colour], BLANK () )
)
)
)
)
)
)
)
)
)
)
)
VAR calc =
IF ( hasFilter, preCalc, BLANK () )
RETURN
calc
How can I correct it?

DAX Default to last value with data

Presently my data returns:
What I need it to do is if the current month has 0's, it will default to the last months value with data:
I know this can be done with nested IF statements, but is there a better way?
UPDATED WITH #TPD SUGGESTION
The results from #TPD suggestion yield:
With measure defined as:
IF([Land Dev Alloc] = 0, CALCULATE([Land Dev Alloc],TOPN(1, CALCULATETABLE(Hyperion,FILTER(ALL(Hyperion), [Land Dev Alloc]>0)),Hyperion[DimDateID],DESC)),[Land Dev Alloc])
Where Hyperion is the main fact table that measure Land Dev Alloc pulls from
I'm not sure if this is the best way but I've recently solved a similar problem like this, assuming you have a Date table and a Value table joined with a relationship:
CurrentOrLastValue:=
CALCULATE (
-- EXTRACT VALUE FROM ROW
FIRSTNONBLANK( 'value'[value], 1 ),
-- FIRST ROW FROM YOUR 'VALUES' TABLE REDUCED TO THOSE BEFORE THE CURRENT CELL DATE
-- ORDERED BY DATE DESC
TOPN (
1,
CALCULATETABLE (
'value',
FILTER
(
ALL( 'date'[date] ),
'date'[date] <= MAX( 'date'[date] )
)
),
'value'[date],
DESC
)
)
Data tables and Pivot Table
Measure
Relationships
UPDATE TO SHOW MEASURE RESULT NOT RAW VALUE
Add a new core measure (doubling values to show a difference):
TotalValue:=SUM('value'[value]) * 2
Add a new measure to show desired output:
CurrentOrLastValueMeasure:=CALCULATE (
[TotalValue],
TOPN(
1,
CALCULATETABLE(
'value',
FILTER(
ALL( 'date'[date] ),
'date'[date] <= MAX( 'date'[date] )
)
),
'value'[date],
DESC
)
)
New measure in pivot table:
UPDATE TO SHOW LAST NON-ZERO VALUE WHEN MEASURE RETURNS ZERO
LastNonZeroMeasure:=
IF( [TotalValue] = 0,
CALCULATE (
[TotalValue],
TOPN(
1,
CALCULATETABLE(
'value',
FILTER(
ALL( 'value' ),
[TotalValue] > 0
)
),
'value'[date],
DESC
)
),
[TotalValue]
)
TotalValue not being doubled anymore. Two data points for 4th Jan to show the measure's aggregation working.
UPDATE TO IGNORE DATES AHEAD OF CELL DATE
Try filtering the dates also...
LastNonZeroMeasure:=IF( [TotalValue] = 0,
CALCULATE (
[TotalValue],
TOPN(
1,
CALCULATETABLE(
'value',
FILTER(
ALL( 'value' ),
[TotalValue] > 0
),
FILTER(
ALL( 'date' ),
'date'[date] < max( 'date'[date] )
)
),
'value'[date],
DESC
)
),
[TotalValue]
)
Ended up being far more simple than originally thought:
MeasureOne BALANCE YTD:= VAR LastNoneblankDate = CALCULATE(max('Date'[DimDateID]),FILTER(ALL('Date'),'Date'[Fiscal_Year] = MAX('Date'[Fiscal_Year])),FILTER(ALL(FactTable),[MeasureOne] > 0)) return IF([MeasureOne]=0, CALCULATE([MeasureOne],FILTER(ALL('Date'),'Date'[DimDateID] = LastNoneblankDate)), [MeasureOne BASE])
Where MeasureOne Base:
MeasureOne BASE:= VAR LastNoneblankDate = CALCULATE(max('Date'[Date]),FILTER(ALL(FactTable),[MeasureOne] > 0)) return IF(HASONEVALUE('Date'[Date]),[MeasureOne], CALCULATE([MeasureOne],FILTER(ALL('Date'),'Date'[Date] = LastNoneblankDate)))
the main issue was setting ALL(FactTable) instead of jut FactTable and handling the cases with

Resources