Why is var inside DAX measure not working? - powerbi

I have my model with 3 tables:
Fact
Project
Roles
I have this measure working fine:
Basic:=
VAR _sumat = [Net Revenue] - [Expected Revenue]
VAR _sumlevel2 =
IF ( _sumat > 0, 0, _sumat )
RETURN
IF ( ISINSCOPE ( Fact[Level3] ), _sumat,
IF ( ISINSCOPE ( Fact[Level2] ), _sumlevel2 ))¨
But on this measure my var _Level0 does not work:
Result:=
VAR _LEVEL1 =
SUMX (SUMMARIZE (
FILTER (ALL ( 'revenue' ),
Fact[L1] = MAX ( Revenue[L1] ) && Fact[Level2] IN VALUES ( Fact[Level2] )
),
Fact[Level1],
Fact[Level2]
),
[Basic])
VAR _LEVEL0 =
SUMX (SUMMARIZE (
FILTER (ALL ( ‘Roles’ ),
Roles[Name] = MAX ( Roles[Name] ) && Fact[Level1] IN VALUES ( Fact[Level1] )
),
Roles[Name],
Fact[Level1]
),
[Basic])
RETURN
SWITCH (
TRUE(),
ISINSCOPE (Fact[Level3] ), [Basic],
ISINSCOPE ( Fact [Level2] ), [Basic],
ISINSCOPE ( Fact [Level1] ), _LEVEL1,
ISINSCOPE ( Roles[Name] ), _LEVEL0,
)
How can I rewrite _LEVEL0 in order to make it work??

Related

Want to show null values in Dax

I have this sentence in DAX:
DEFINE
MEASURE 'BUYING SHOP'[FromDate] =
CALCULATETABLE (
DATEADD ( 'BUYING SHOP'[FROM_DATE], -1, YEAR ),
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) )
)
MEASURE 'BUYING SHOP'[ToDate] =
CALCULATE (
[Prior Completed Month],
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) )
)
MEASURE 'PRODUCT'[Maintenance_Repair] =
CALCULATE (
SUMX (
FILTER (
PRODUCT,
PRODUCT[PRODUCT_CATEGORY_CODE] = "MAINTENANCE_AND_REPAIR"
),
PURCHASE[Sum Purchases Prior]
)
)
EVALUATE
SUMMARIZECOLUMNS (
'DATE'[cMonth],
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) ),
KEEPFILTERS (
FILTER (
ALL ( 'DATE'[FullDate] ),
'DATE'[FullDate] >= 'BUYING SHOP'[FromDate]
&& 'DATE'[FullDate] <= 'BUYING SHOP'[ToDate]
)
),
"Maintenance_Repair", [Maintenance_Repair]
)
ORDER BY 'DATE'[cMonth] ASC
that returns this table:
enter image description here
What I want is that all months appear in the table even if it does not have any record.
for example:
03-Mar 20.8
04-Apr 0
05-May 222.04
06'Jub 0
and goes like that
Could anybody help me?
Try changing your measure to add zero.
MEASURE 'PRODUCT'[Maintenance_Repair] =
CALCULATE (
SUMX (
FILTER (
PRODUCT,
PRODUCT[PRODUCT_CATEGORY_CODE] = "MAINTENANCE_AND_REPAIR"
),
PURCHASE[Sum Purchases Prior]
)
) + 0

DAX New & Lost Customers Table

I am trying to create a List for New Customers Added during the year and List of Lost Customers, I have written a DAX which works fine in summary count but doesn't work in table matrix.
NTB =
VAR currentCustomers =
VALUES ( Deposits[CIF ID] )
VAR currentDate =
MAX ( Deposits[Source.Date] )
VAR pastCustomers =
CALCULATETABLE (
VALUES ( Deposits[CIF ID] ),
ALL (
Deposits[Source.Date].[Month],
Deposits[Source.Date].[MonthNo],
Deposits[Source.Date].[Year]
),
Deposits[Source.Date] < currentDate
)
VAR newCustomers =
EXCEPT ( currentCustomers, pastCustomers )
RETURN
COUNTROWS ( newCustomers )
Total Row is correct, even if I remove one function the table remains same..
Appreciate your help
try this :
Modelling --> Add Table
Table =
VAR _max =
MAX ( Deposits[Source.Date] )
RETURN
ADDCOLUMNS (
SUMMARIZE ( Deposits, Deposits[CIF ID] ),
"NTB",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] >= _max
)
),
"Lost Customers",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] < _max
)
)
)

Dax measure to correctly calculate previous week category and subtotal

I am using a matrix table in powerbi to show previous week totals for different areas (categories). I have the majority of it working but I am not able to correctly get the subtotals on the table working.
I believe it has to do with the filtering that I am using - i have been unable to correct it.
screen capture
As you can see my Total for week 24 previous is missing
Dax code is:
VAR Area =
MAX ( 'SumTable'[Area Name] )
VAR CurrentWeek =
SELECTEDVALUE ( SumTable[WeekofYear] )
VAR CurrentYear =
SELECTEDVALUE ( SumTable[Year] )
VAR MaxWeekNumber =
CALCULATE ( MAX ( SumTable[WeekofYear] ), ALL ( SumTable ) )
RETURN
IF (
HASONEVALUE ( SumTable[Area Name] ),
SUMX (
FILTER (
ALL ( SumTable ),
IF (
CurrentWeek = 1,
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1
&& SumTable[Area Name] = Area,
SumTable[WeekofYear] = CurrentWeek - 1
&& SumTable[Year] = CurrentYear
&& SumTable[Area Name] = Area
)
),
SumTable[BOE]
),
SUMX (
FILTER (
ALLSELECTED ( SumTable ),
IF (
CurrentWeek = 1,
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1,
SumTable[WeekofYear] = CurrentWeek - 1
&& SumTable[Year] = CurrentYear
)
),
SumTable[BOE]
)
)
Data Table:
Example Table Format
Thank you, first time poster!
B
I would start by spliting my data table from my date table.
And I guess you don't need to ALL the whole table, just the columns for year and weeknumber and keep the Area in context, that way you don't have to bother if HASONEVALUE it will just work.
SELECTEDVALUE only returns if only a single value for that column is in context, not the case for totals and subtotals.
MyMeasure =
VAR CurrentWeek =
MAX( SumTable[WeekofYear] )
VAR CurrentYear =
MAX( SumTable[Year] )
VAR MaxWeekNumber =
CALCULATE ( MAX ( SumTable[WeekofYear] ), SumTable[Year] = CurrentYear-1 )
RETURN
IF(
CurrentWeek = 1,
CALCULATE(
SUM(SumTable[BOE]),
FILTER (
ALL ( SumTable[Year],SumTable[WeekofYear]),
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1
)
),
CALCULATE (
SUM ( SumTable[BOE] ),
FILTER (
ALL ( SumTable[WeekofYear] ),
SumTable[WeekofYear] = CurrentWeek-1
)
)
)
I did not have a chance to confirm this code.

multiple results- the query fails

I am trying to link between header table and the fact table in order to get correct values:
Formules =
VAR Top1 = SELECTEDVALUE ( EnteteRapportAgentClient[Top] )
VAR Middle = SELECTEDVALUE ( EnteteRapportAgentClient[Middle] )
VAR BottomIndex = SELECTEDVALUE ( EnteteRapportAgentClient[Index3] )
VAR a = SELECTEDVALUE ( Dim_DateFicheAgent[ID_DateFicheAgent] )
VAR b = SELECTEDVALUE ( 'Seniority banking'[banking seniority] )
VAR Bottom = SELECTEDVALUE ( EnteteRapportAgentClient[Bottom] )
VAR Val =
SWITCH (
TRUE (),
Top1 = "Nombre de leads", [Lead] + 0,
Top1 = "Affaires nouvelles"
&& BottomIndex <> 0, CALCULATE (
COUNTROWS (
FILTER (
Fact_AN,
(
Fact_AN[banking seniority] <= b
&& NOT ISBLANK ( Fact_AN[banking seniority] )
&& Fact_AN[Code_Produit ]
= LOOKUPVALUE (
Dim_Produit[Code_Produit ],
Dim_Produit[Dim5Rapport], Middle,
Dim_Produit[Dim6Rapport], Bottom
)
)
)
),
DATESBETWEEN (
Dim_DateFicheAgent[ID_DateFicheAgent],
NEXTDAY (
SAMEPERIODLASTYEAR (
LASTDATE ( Dim_DateFicheAgent[ID_DateFicheAgent] )
)
),
LASTDATE ( Dim_DateFicheAgent[ID_DateFicheAgent] )
)
),
Middle = "Affaires nouvelles", [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
I get an error
I put here a pbix file. https://drive.google.com/file/d/1OwE52NRyq_W13u2N84pnNVw1lnatmOSw/view?usp=drivesdk
There's a lot going on there but that particular error likely comes from LOOKUPVALUE not returning a unique value.

Slow Measure/Visual when using SelectedValue

I'm trying to create a report that will allow my users to make selection based on the total of a column and date. So I created a table, one column, values are 1 to 20. Also a date selection, today plus 14 days back. Before when I asked to give me all devices with 4 or more outages or Date(2019,07,17), it loaded my visual within 20 seconds. Now that I'm using selectedvalue(), its taking my visual 9min and 54 seconds to load. What is the problem?
This takes 10mins
Repeat Devices Outage Count =
var numberSelect = SELECTEDVALUE(Numbers[Numbers])
VAR datesSelection = SelectedValue(Date Selection[DateWoTime])
VAR devicesTotal =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection,
VALUES ( Outages[Feeder_Device] )
)
var caseTotal = CALCULATE (
COUNT ( Outages[CASE_ID] ),
ALLSELECTED ( Outages ),
Outages[Feeder_Device] IN devicesTotal)
VAR devices =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection
)
RETURN
SWITCH(TRUE(),
caseTotal >= numberSelect,
CALCULATE (
COUNT ( Outages[DEVICE_ID] ),
FILTER ( Outages, Outages[Feeder_Device] IN devices )
),
BLANK()
)
This takes 20 seconds or less.
Repeat Devices Outage Count =
var numberSelect = 4
VAR datesSelection = Date(2019,07,17)
VAR devicesTotal =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection,
VALUES ( Outages[Feeder_Device] )
)
var caseTotal = CALCULATE (
COUNT ( Outages[CASE_ID] ),
ALLSELECTED ( Outages ),
Outages[Feeder_Device] IN devicesTotal)
VAR devices =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection
)
RETURN
SWITCH(TRUE(),
caseTotal >= numberSelect,
CALCULATE (
COUNT ( Outages[DEVICE_ID] ),
FILTER ( Outages, Outages[Feeder_Device] IN devices )
),
BLANK()
)
Why is selectedvalue() slowing my report down so much?
Unless you've used something like DAX STUDIO to clock each element in the measure I wouldn't assume SELECTEDVALUE() is the culprit. I would start by eliminating unnecessary FILTER calls since these are essentially creating entire tables in memory upon execution (every time). Try this:
Repeat Devices Outage Count =
VAR numberSelect =
SELECTEDVALUE ( Numbers[Numbers] )
VAR datesSelection =
SELECTEDVALUE ( 'Date Selection'[DateWoTime] )
VAR devicesTotal =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection,
VALUES ( Outages[Feeder_Device] )
)
VAR caseTotal =
CALCULATE (
COUNT ( Outages[CASE_ID] ),
ALLSELECTED ( Outages ),
Outages[Feeder_Device] IN devicesTotal
)
VAR devices =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection
)
RETURN
SWITCH (
TRUE (),
caseTotal >= numberSelect,
CALCULATE ( COUNT ( Outages[DEVICE_ID] )
, Outages[Feeder_Device] IN devices ),
BLANK ()
)
Or this (which is equivalent just more readable):
Repeat Devices Outage Count =
VAR numberSelect =
SELECTEDVALUE ( Numbers[Numbers] )
VAR datesSelection =
SELECTEDVALUE ( 'Date Selection'[DateWoTime] )
VAR devicesTotal =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection,
VALUES ( Outages[Feeder_Device] )
)
VAR caseTotal =
CALCULATE (
COUNT ( Outages[CASE_ID] ),
ALLSELECTED ( Outages ),
Outages[Feeder_Device]
IN devicesTotal
)
VAR devices =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection
)
RETURN
SWITCH (
TRUE (),
caseTotal >= numberSelect, CALCULATE (
COUNT ( Outages[DEVICE_ID] ),
FILTER (
ALL ( Outages[Feeder_Device] ),
Outages[Feeder_Device]
IN devices
)
),
BLANK ()
)
This article: FILTER ARGUMENTS IN CALCULATE goes over the dangers in using FILTER on entire tables.
So I've adjusted my code and this works. This was the closest thing to a 'Having' function that I could think of. Now my report is running back to normal. I guess it wasn't the selected values, because "Outages[DATE] >= datesSelection" worked like it should have. So I'm assuming it had to do something with the 'IF' and 'Switch' function.
Repeat Devices Outage Count =
var numberSelect = SELECTEDVALUE(Numbers[Numbers])
VAR datesSelection = SELECTEDVALUE('Date Selection'[Date])
VAR devicesTotal =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection,
VALUES ( Outages[Feeder_Device] )
)
var caseTotal = CALCULATE (
COUNT ( Outages[CASE_ID] ),
ALLSELECTED ( Outages ),
Outages[Feeder_Device] IN devicesTotal)
VAR devices =
CALCULATETABLE (
VALUES ( Outages[Feeder_Device] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection
)
RETURN
CALCULATE (
COUNT ( Outages[DEVICE_ID] ),
FILTER ( Outages, Outages[Feeder_Device] IN devices ),
FILTER(Outages, caseTotal >= numberSelect)
)