DAX New & Lost Customers Table - powerbi

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
)
)
)

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

Why is var inside DAX measure not working?

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??

Power Bi: Top N visual level filter as Measure

Need a measure that will provide the same output as TopN visual level filter (than I can parameterize it).
The solution for simple cases provided HERE
But it doesn't work for more complicated cases...
EXAMPLE:
Don't work if you add any dimension that has Many to One Product Name relationship (Order Number for example).
Desired output: both tables (top and bottom) should be equal:
Example from screen available here HERE
NB! From usability perspective it's preferable to return Sales Rank in measure.
Here you go.
BottomN =
VAR param = [TopN Value]
VAR topNTable = CALCULATETABLE( TOPN(param,'Product', [Sales Amount], ASC), ALLSELECTED('Product'[Category],'Product'[Product Name]), FILTER(allselected(Sales), [Sales Amount] <> BLANK()))
RETURN
IF(NOT(ISEMPTY(Sales)),IF( SELECTEDVALUE('Product'[Product Name]) IN SELECTCOLUMNS( topNTable,"a", 'Product'[Product Name]) ,[Sales Amount]))
A bit reworked solution from David Bacci:
1. If you need just TopN Sales:
TopnSalesAmount =
VAR param = [TopN Value]
VAR topNTable =
CALCULATETABLE (
TOPN ( param, 'Product', [Sales Amount], ASC ),
ALLSELECTED ( 'Product'[Product Name] ),
FILTER ( ALLSELECTED ( Sales ), [Sales Amount] <> BLANK () )
)
RETURN
IF (
NOT ( ISEMPTY ( Sales ) ),
IF (
SELECTEDVALUE ( 'Product'[Product Name] )
IN SELECTCOLUMNS ( topNTable, "a", 'Product'[Product Name] ),
[Sales Amount]
)
)
2. If you need Rank for TopN Sales:
rnkTopnSalesAmount =
IF (
//ISINSCOPE ( 'Product Names'[Product Name]), -- depends, which one is used in visual
ISINSCOPE ( 'Product'[Product Name] )
&& NOT ( ISEMPTY ( Sales ) ),
VAR ProductsToRank = [TopN Value]
VAR topNTable =
CALCULATETABLE (
TOPN (
ProductsToRank,
ADDCOLUMNS ( VALUES ( 'Product'[Product Name] ), "#Amt", [Sales Amount] ),
[Sales Amount], ASC
),
FILTER ( ALLSELECTED ( Sales ), [Sales Amount] <> BLANK () )
)
RETURN
IF (
SELECTEDVALUE ( 'Product'[Product Name] )
IN SELECTCOLUMNS ( topNTable, "a", 'Product'[Product Name] ),
RANKX ( topNTable, [#Amt], [Sales Amount], ASC )
)
)

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.

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)
)