Power BI, DAX, Many-to-one and relational tables - powerbi

I previously asked a question here:
DAX subquery measure?
for instruction on how to create a specific measure column for a visualisation. To keep the example simple, I kept it to one fictitious table and the DAX query worked really well.
In reality, however, the visualisation that the measure column is for is made up of multiple joined tables. And the results of the DAX query unexpectedly produced all zeros! So I'll refactor my example here for more help...
Requirement
I want a count of how many 'Apps' are not equal to 'Complete' for a specific 'Build'.
Data Model
Builds
Build
App
Apps
App
Status
Sample Data
Builds
Build...........App
Build1..........App1
Build1..........App2
Build1..........App9
Build2..........App3
Build3..........App1
Build3..........App5
Build3..........App8
Build3..........App9
Apps
App...........Status
App1..........UAT
App2..........Complete
App9..........New
App3..........Complete
App5..........UAT
App8..........Complete
Relationship
The relationship is MANY Builds.App to ONE Apps.App.
Visualisation Table
This is my visualisation - note the different tables:
Builds.Build....Builds.App....Apps.Status
Build1..........App1..........UAT
Build1..........App2..........Complete
Build1..........App9..........New
Build2..........App3..........Complete
Build3..........App1..........UAT
Build3..........App5..........UAT
Build3..........App8..........Complete
Build3..........App9..........New
This is my required results:
Builds.Build....Builds.App....Apps.Status....AppsNotCompleteForBuild
Build1..........App1..........UAT............2
Build1..........App2..........Complete.......2
Build1..........App9..........New............2
Build2..........App3..........Complete.......0
Build3..........App1..........UAT............3
Build3..........App5..........UAT............3
Build3..........App8..........Complete.......3
Build3..........App9..........New............3
ATTEMPT 1 (Not working!)
CALCULATE (
COUNT ( Builds[App] ),
FILTER (
ALL ( Builds[Build], Builds[App] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
),
FILTER (
ALL ( Apps[Status] ),
Apps[Status] <> "Complete"
)
) + 0
ATTEMPT 2 (Not working!)
Measure 5 = CALCULATE (
COUNT ( Builds[App] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
),
FILTER (RELATEDTABLE(Apps),
Apps[Status] <> "Complete")
) + 0
ATTEMPT 3 (Not working!)
Measure5 = CALCULATE (
COUNTAX(FILTER( Builds
, RELATED(Apps[Status]) <>"Complete"
&& Builds[Build] = SELECTEDVALUE(Builds[Build])
)
,Builds[App])
) + 0

using these tables with a many to one relationship between Builds and Apps
Builds =
DATATABLE(
"Build", STRING,
"App", STRING,
{
{ "Build1", "App1" },
{ "Build1", "App2" },
{ "Build1", "App9" },
{ "Build2", "App3" },
{ "Build3", "App1" },
{ "Build3", "App5" },
{ "Build3", "App8" },
{ "Build3", "App9" }
}
)
Apps =
DATATABLE(
"App", STRING,
"Status", STRING,
{
{ "App1", "UAT" },
{ "App2", "Complete" },
{ "App9", "New" },
{ "App3", "Complete" },
{ "App5", "UAT" },
{ "App8", "Complete" }
}
)
we can write a dax measure that counts the number of apps per build that are not in "Complete" status. Since an app can have just one status, otherwise the many to one relationship would break, it's enough to filter out status = "Complete" when counting.
# not complete =
IF(
HASONEVALUE( Builds[Build] ),
VAR CurrentBuild =
SELECTEDVALUE( Builds[Build] )
RETURN
COUNTROWS(
FILTER(
ALL( Builds ),
Builds[Build] = CurrentBuild
&& RELATED( Apps[Status] ) <> "Complete"
)
) + 0
)
With this formula we can use a Table Visual to get this result
Edit: this will also handle cases where there are missing Apps in Apps table, just ignoring them
# not complete =
IF(
HASONEVALUE( Builds[Build] ),
VAR CurrentBuild =
SELECTEDVALUE( Builds[Build] )
VAR CurrentApp =
SELECTEDVALUE( Apps[App] )
VAR Result =
COUNTROWS(
FILTER(
ALLNOBLANKROW( Builds ),
Builds[Build] = CurrentBuild
&& RELATED( Apps[Status] ) <> "Complete"
&& NOT ISBLANK( RELATED( Apps[Status] ) )
)
) + 0
RETURN
IF( NOT ISBLANK( SELECTEDVALUE( Apps[Status] ) ), Result )
)

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

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

Count unique occurrences within a year

My database schema looks like down below
ID
Date
Status
ID1
2022/01/01
Active
ID1
2022/02/01
Active
ID1
2022/03/01
Active
ID1
2022/04/01
Terminated
ID2
2022/01/01
Active
ID2
2022/02/01
Terminated
I'd like to calculate unique occurrences from start of selected date year, till the selected date. My formula is:
CountOfUnique = CALCULATE( DISTINCTCOUNT( 'Table'[ID] ) , 'Table'[STATUS] = "Active", DATESBETWEEN('CALENDAR'[DATE], STARTOFYEAR('CALENDAR'[DATE]), MAX('CALENDAR'[DATE]) ))
In SQL I'd need something like
SELECT COUNT ( DISTINCT ID) FROM Table
WHERE STATUS = "ACTIVE"
AND DATE BETWEEN 2022/01/01 AND 2022/04/01
Try this:
CountOfUnique =
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID] ),
'Table'[STATUS] = "Active",
DATESBETWEEN (
'CALENDAR'[DATE],
STARTOFYEAR ( 'CALENDAR'[DATE] ),
SELECTEDVALUE ( 'CALENDAR'[DATE] )
)
)
when you have a slicer on the visual, the start of selected date year doesnt mean much as you select the dates on the slicer. I created a Calendar Table = CALENDARAUTO() so it started from the 2022/01/01...
use one of these as you like...
sample PBix File
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
or only Active if you need
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)

Power BI Dax - Trying to break circular dependency after one attempt

I am trying to figure out ancestor bloodline in data I have. I feel like there is something I am missing to make this work. This data wouldn't change so not sure if I am missing something I can write in the power query editor when loading / refreshing the data.
While technically it is circular in fields it never going to return the same row it is currently in and the first generation is hard number making a starting point. I only want to reference the mother and father to calculate the child's bloodline. The first generation is a basic IF() statement. Below is as far as I can get before hitting the circular dependency error. I have tried a few things to break it thinking its going to loop.
Logic is:
Each blood is 100% for 1st generations based on their birthplace then it is ((mother blood + father blood) / 2) for each generation after that. I found I can use PATHITEM() to isolate the type of blood but errors with a circular dependency. (This is where I can't figure out how to reference the mother / father to do the calculation.) If I take this part out I get the image below working for 1st generation and correct mother / father for second generation.
Asisa Blood =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR current_blood = 'Sheet1'[Birthplace]
VAR current_mother_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Mother's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR current_father_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Father's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR gen1_value = 100
RETURN
IF(AND(LOWER(current_gen) = "1",LOWER(current_blood) = "asisa"),
gen1_value,
((current_mother_blood + current_father_blood)/2)
)
Blood Mix concatenates the four blood types into one field for easy look up in next step.
Blood mix =
VAR current__id = 'Sheet1'[ID]
VAR current_blood_a = 'Sheet1'[Asisa Blood]
VAR current_blood_b = 'Sheet1'[Africa Blood]
VAR current_blood_c = 'Sheet1'[Europe Blood]
VAR current_blood_d = 'Sheet1'[North America Blood]
RETURN
current_blood_a & "|" & current_blood_b & "|" & current_blood_c & "|" & current_blood_d
Mother and Father are lookups on blood mix with mother or father ids
Mother's Blood Mix =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR gen_value = 'Sheet1'[Blood mix]
VAR current_parent_id =
IF(LOWER(current_gen) = "1",current_id,'Sheet1'[Mother ID])
VAR result =
CALCULATE(
DISTINCT('Sheet1'[Blood mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_parent_id
),
REMOVEFILTERS('Sheet1')
)
RETURN
result
You can try to do this way (rather high resource consuming):
NEW COLUMN:
heritage_Father = PATH(Blood[ID],(Blood[FatherID]))
heritage_Mother = PATH(Blood[ID],(Blood[MotherID]))
Mancestor = LOOKUPVALUE(Blood[heritage_Mother],Blood[ID],Blood[FatherID]) &"|"& Blood[heritage_Mother]
Fancestor = LOOKUPVALUE(Blood[heritage_Father],Blood[ID],Blood[MotherID])&"|"&Blood[heritage_Father]
NEW MEASURE:
ASIA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AsiaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))
AFRICA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AfricaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))