Want to show null values in Dax - powerbi

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

Related

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

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

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.

Power BI - Matrix count of values in query

I am trying to create a matrix table that will count the number of jobs due in a month and also if they are completed on time.
At present I can get it this far.
I have tried summarizing data, creating measures etc. But to no avail.
I need to produce the following:
My data source is a series of transactions lines that include
1) Transaction Due date
2) Transaction completed date
Definition of terms:
Due in month Count of due date
Done on time Completed within due month
Outside time completed outside due month
"%Perf" percentage completed on time.
Any help with this would be much appreciated.
Add the following measures:
Measure "Due":
Due = COUNTROWS ( Table1 )
Measure "Done on time":
Done on time =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
MONTH ( Table1[completed] ) = DueMonth
)
)
Measure "Outside time":
Outside time =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
MONTH ( Table1[completed] ) <> DueMonth &&
NOT ISBLANK ( Table1[completed] )
)
)
Measure "Incomplete":
Incomplete =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
ISBLANK ( Table1[completed] )
)
)
Measure "% Perf"
% Perf =
DIVIDE (
[Done on time],
[Due],
BLANK()
)
Group by due_date.month in report.
See https://pwrbi.com/so_55513978/ for worked example PBIX file.
EDIT after updated question in comment:
There are many ways you could approach creating a visualisation which combines these measures, with the monthly counts by completion date, if you really must. Here's one approach:
Create a "Report Columns" table:
Report Columns =
VAR ListMeasures =
DATATABLE (
"Header", STRING,
"Sort Order", INTEGER,
"First Of Month", DATETIME,
{
{"Due in month", 1, BLANK() },
{"Done on time", 2, BLANK() },
{"Outside time", 3, BLANK() },
{"Incomplete", 4, BLANK() },
{"% Perf", 5, BLANK() }
}
)
VAR ListMonths =
CALCULATETABLE(
GROUPBY (
ADDCOLUMNS (
DISTINCT ( Table1[completed] ),
"Header", FORMAT ( Table1[completed], "YYYY-MMM" ),
"Sort Order", YEAR ( Table1[completed] ) * 100 + MONTH ( Table1[completed] ),
"First Of Month", DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 )
),
[Header], [Sort Order], [First Of Month]
),
Table1[completed] <> BLANK()
)
RETURN
UNION (
ListMeasures,
ListMonths
)
Set column Header to Sort By column Sort Order
Create Measure "Report Measure":
Report Measure =
IF (
NOT HASONEFILTER ( 'Report Columns'[Header] ),
BLANK(),
SWITCH (
VALUES ( 'Report Columns'[Header] ),
"Due in month", [Due],
"Done on time", [Done on time],
"Outside time", [Outside time],
"Incomplete", [Incomplete],
"% Perf", [% Perf],
CALCULATE (
[Due],
FILTER (
Table1,
DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 ) = VALUES ( 'Report Columns'[First Of Month] )
)
)
)
)
Add a matrix visualisation, with Table1[due_date] in rows, Report Columns[Header] in Columns, and [Report Measure] in values. Format to suit.
Updated example PBIX file: https://pwrbi.com/so_55513978-2/