wrong values with customized double header in matrix - powerbi

I use a custom calculated table for the header which was an answer of a my previous question: https://stackoverflow.com/a/61469905/5950313
The measure AN is calculated within the following script:
The goal of the dimensionmeasure is to calculate the count of rows from fact_an cumul of 12 months where
Fact_AN[Vitesse_Transf_Mois]<=
SELECTEDVALUE(Dim_VieillissementAN[ID_Tranche])
AN =
VAR a = SELECTEDVALUE(Dim_DateFicheAgent[ID_DateFicheAgent])
VAR b =SELECTEDVALUE('Seniority banking'[banking seniority])
RETURN
CALCULATE(
COUNTROWS(FILTER(Fact_AN;
(Fact_AN[banking seniority]<=b && NOT ISBLANK (Fact_AN[banking seniority]))));
DATESBETWEEN (
Dim_DateFicheAgent[ID_DateFicheAgent];
NEXTDAY ( SAMEPERIODLASTYEAR (LASTDATE ( Dim_DateFicheAgent[ID_DateFicheAgent] ) ));
LASTDATE ( Dim_DateFicheAgent[ID_DateFicheAgent] )
))
the measure DimensionMeasure returns wrong values, it's almost the same value for all middle in the matrix.
How to correct it?
I use a star schema which means; I have only one fact table fact_an.
The table fact is linked to dim_produit by code_produit.
The description of the table dim_produit:
Codeproduit Dim5Rapport Dim6rapport
I try
Formules =
VAR Top1 = SELECTEDVALUE ( EnteteRapportAgentClient[Top] )
VAR Middle = SELECTEDVALUE ( EnteteRapportAgentClient[Middle] )
VAR BottomIndex = SELECTEDVALUE ( EnteteRapportAgentClient[Index3] )
VAR Val =
SWITCH (
TRUE ();
Top1 = "Nombre de leads"; [Lead]+ 0;
Top1 = "Affaires nouvelles" && BottomIndex <> 0; [AN]+0;
Middle = "Total AN";[AN]+ 0;
Middle = "Taux Transfo"; DIVIDE([AN];[Lead])
)
VAR ValF=
IF( Middle = "Taux Transfo";
FORMAT ( Val; "0.0%" );
FORMAT ( Val; "0" ))
VAR Val2=
IF (ValF="0";"";ValF
)RETURN Val2
But it returns always error. I put an example here https://drive.google.com/file/d/1i5HEnpoJ5mgEl98xUZzPFo7D6S0C-_tm/view?usp=drivesdk
The wrong values is for AN it returns the same value everywhere
here are the expected results:

The key here is that you need to pass the header context as filter context to your measure.
For example, instead of the line
Top1 = "Affaires nouvelles" && BottomIndex <> 0, [AN]+0,
You need to replace that [AN] with
CALCULATE ( [AN], Dim_Produit[Dim5Rapport] = Middle, Dim_Produit[Dim6Rapport] = Bottom )
where
VAR Middle = SELECTEDVALUE ( EnteteRapportAgentClient[Middle] )
VAR Bottom = SELECTEDVALUE ( EnteteRapportAgentClient[Bottom] )
The same goes for all of the different calculations. You need to set the filter context that you'd expect if the header were based on your actual dimensions rather than an artificial constructed header.

Related

arguments in generate series cannot be blank error

i am trying to apply levenshtein distance to 2 columns, tables and getting error in generate series cannot be blank.Here is my code.
Measure =
VAR SlicerText =
SELECTEDVALUE ( 'Slicer Table'[TestColumn] )
VAR TableText =
SELECTEDVALUE ( 'Table'[TestColumn] )
VAR length =
MAX ( LEN ( SlicerText ), LEN ( TableText ) )
VAR TestTable =
ADDCOLUMNS (
GENERATESERIES ( 1, length, 1 ),
"InSlicer", MID ( SlicerText, [Value], 1 ),
"InTable", MID ( TableText, [Value], 1 )
)
RETURN
COUNTROWS ( FILTER ( TestTable, [InSlicer] = [InTable] ) )
/ COUNTROWS ( TestTable )
With a little effort you could have figured this out yourself:
The error message tells you that one of the parameters of GENERATESERIES() is blank. This can ONLY be the length variable.
length is blank when both variables SlicerText and TableText are blank.
both variables refer to the SELECTEDVALUE() function, which returns blank if none or more than one rows are selected and you didn't provide an alternateResult, see the official documentation here: SELECTEDVALUE
Bottom line: Make shure you have exactly one row selectd in 'Slicer Table' and 'Table'.
This is as much help as you can get, since you didn't share a minimal, reproducible example as recommended by StackOverflow.

Measure Including Year Filter

i have done a Measure to get the Revenue from TOP N Products.
N depends on the selected Value from my TOP-N Table (It's containing just the numbers from 1 to 10).
In the same Measure, I also calculate how much was the revenue of all Products how were not in the selected Top N.
The Code for this is:
Top N Produktbez Sum DB3 =
VAR TopNSelected =
SELECTEDVALUE ( 'TOP N-Filter'[TOP N] )
VAR TopNProdbezTable =
TOPN ( TopNSelected, ALLSELECTED ( 'Pseudo Produkbez Table' ), [DB3] )
VAR TopNProdbez =
CALCULATE ( [DB3], KEEPFILTERS ( TopNProdbezTable ) )
VAR OtherSales =
CALCULATE ( [DB3], ALLSELECTED ( 'Pseudo Produkbez Table' ) )
- CALCULATE ( [DB3], TopNProdbezTable )
VAR CurrentProd =
SELECTEDVALUE ( 'Pseudo Produkbez Table'[Produktbezeichnung] )
RETURN
IF ( CurrentProd <> "Others", TopNProdbez, OtherSales )
Now I want to add Year as Filter.
I'm using the Year coming from the Date Hierarchy.
Unfortunately, I can't provide a sample yet.
If a sample is need, I will try to create own.

How to get previous row value of a text column in new column in Dax -Power BI

In PowerBI, I'm trying to get a calculated column (not a measure) that gets preivous row value. I tried adding a column with DAX formula as below:
DAX Index = RANKX(ALL( InventoryItems ),InventoryItems[INVENTORYDATE],,ASC,Dense)
STATUSyesterday = VAR _CurrentRowIndex = InventoryItems[DAX Index] VAR _PreviousRowIndex = CALCULATE(MAX( InventoryItems[DAX Index] ),FILTER( InventoryItems, InventoryItems[DAX Index] <_CurrentRowIndex ),ALL( InventoryItems[DAX Index] ))VAR _Result = CALCULATE(MAX(InventoryItems[STATUSinventory] ),FILTER( InventoryItems, InventoryItems[DAX Index]=_PreviousRowIndex ))RETURN _Result
Ideally, result of STATUSyesterday should first row blank and from second row onwards it should be STATUSinventory instead, its just 'out of stock' as it is occurring maximum time.
query image
Try this and let me know if it works. What I am doing is getting the id for the previous row and the matching the "STATUSinventory" with the previous id.
Previous Row Value =
VAR _PreviousRow =
CALCULATE (
MAX ( 'InventoryItems'[DAX Index] ),
ALL ( 'InventoryItems' ),
'InventoryItems'[ITEMNUMBER]
= EARLIER ( 'InventoryItems'[ITEMNUMBER] ),
'Summary Table'[Date] < EARLIER ( 'InventoryItems'[INVENTORYDATE] )
)
RETURN
CALCULATE (
LASTNONBLANK ( 'InventoryItems'[STATUSinventory], 1 ),
ALL ('InventoryItems' ),
'InventoryItems'[DAX Index] = _PreviousRow
)
It is not necessary to add an index. You can use the Inventorydate to determine the order. Try something like this:
STATUSyesterday =
VAR currentInventoryDate = 'InventoryItems'[INVENTORYDATE]
VAR previousInventoryDate =
CALCULATE (
MAX ( 'InventoryItems'[INVENTORYDATE] ),
ALL ( 'InventoryItems' ),
'InventoryItems'[INVENTORYDATE] < currentInventoryDate
)
RETURN
CALCULATE (
MAX ( 'InventoryItems'[STATUSinventory] ),
ALL ( 'InventoryItems' ),
'InventoryItems'[INVENTORYDATE] = previousInventoryDate
)
[currentInventoryDate] is the Inventorydate in the current row.
[previousInventoryDate] is the max(IventoryDate) that is smaller than the [currentInventoryDate].
The last CALCULATE returns the STATUSinventory of the previousInventoryDate.

Calculate Column Sum of a Generated Table - DAX

I've been trying to get the sum of the following table's column VALUE using DaxStudio in order to put that on PowerBI once is's ok, since PBI can get slow if you test a code for large calculated tables.
Table from DaxStudio
BU VALUE
------------------------
FOODS 0.0000
FIBI 0.0000
GEOS/CIS 0.7300
CASC
S_S
SGS
COCOA
COCOA/SSSA
CORPORATE
N/A
CIS
The code behind it:
DEFINE
VAR TOTAL =
CALCULATE (
SUMX (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR]
);
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
)
)
EVALUATE
SUMMARIZE (
CALCULATETABLE (
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> 0
)
);
PACKAGING_POWERBI_YEARLY_2[BU];
"VALUE"; FORMAT (
(
CALCULATE (
SUMX (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR]
);
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
)
) / TOTAL
)
* (
CALCULATE (
SUMX ( PACKAGING_POWERBI_YEARLY_2; PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] );
FILTER (
PACKAGING_POWERBI_YEARLY_2;
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
)
)
);
"0.0000"
)
)
Instead of generating the table I would like to sum the results of column VALUE, but the only way I got to plot a result without error is through a "CALCULATED TABLE" (above).
Every filter I try leads to an error.
The idea is to get just the sum of that calculated column VALUE:
0.7300
But every simpler filter or SUMX condition I try, an error pops-up.
I'd think you could clean it up to look something like this:
CALCULATE (
SUMX (
VALUES ( PACKAGING_POWERBI_YEARLY_2[BU] ),
SUM ( PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR] )
/ CALCULATE (
SUM ( PACKAGING_POWERBI_YEARLY_2[QUANTIDADE_ANTERIOR] ),
ALL ( PACKAGING_POWERBI_YEARLY_2[BU] )
)
* SUM ( PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] )
),
FILTER (
PACKAGING_POWERBI_YEARLY_2,
PACKAGING_POWERBI_YEARLY_2[DATE] = "2018"
&& PACKAGING_POWERBI_YEARLY_2[CODIGO] = "43130"
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> BLANK ()
&& PACKAGING_POWERBI_YEARLY_2[PRECO_PONDERADO] <> 0
)
)
This uses SUMX to iterate through each of the BU values and for each one, calculates the value
(QUANTIDADE_ANTERIOR / Total QUANTIDADE_ANTERIOR ) * PRECO_PONDERADO
where the filters are reused instead of repeated.
I can't guarantee that this code works exactly as intended, but it should point you in a better direction.

DAX if else for measure

How to use if else for DAX in the measure. If row value =1 then take the var a calculated value else take the var b calculated value
x:=var a=[DATA1]
var b=[DATA2]
return(if([HOUR]=1),a,b)
I get error using above formula
It seems your problem is that you are not aggregating the columns while creating the measure. Measures only works aggregating data in a given context, generally if you want to perform calculations per row you should use a calculated column instead of a measure.
And the DAX expression for a calculated column should be:
MyColumn = IF([HOUR] = 1, [DATA1], [DATA2])
Otherwise if you want to use a measure you have to explicitely aggregate the column values in the given context, i.e:
MyMeasure =
VAR a =
FIRSTNONBLANK ( ExampleTable[Data1], 0 )
VAR b =
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
RETURN
IF ( SUM ( ExampleTable[Hour] ) = 1, a, b )
Or simply:
MyMeasure =
IF (
SUM ( [Hour] ) = 1,
FIRSTNONBLANK ( ExampleTable[Data1], 0 ),
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
)
Let me know if this helps.