PowerBI Record Changes - powerbi

I have a table in PowerBI.
Table-
I need to create a custom column, that detects this flag changes.
Output should look like -
e.g= EID 3 changed flag in 2020. thats why new flag is set to 1.
this is a sample , I have multiple eids.

I'd recommend checking out this post to see different approaches for looking up previous flags.
One particular implementation might look like this (but there are lots of other ways to do the same thing):
New Flag =
VAR CurrYear = Table1[Year]
VAR PrevYear =
CALCULATE (
MAX ( Table1[Year] ),
ALLEXCEPT ( Table1, Table1[EID] ),
Table1[Year] < CurrYear
)
VAR PrevFlag =
CALCULATE (
SELECTEDVALUE ( Table1[Flag] ),
ALLEXCEPT ( Table1, Table1[EID] ),
Table1[Year] = PrevYear
)
RETURN
IF ( ISBLANK ( PrevYear ) || Table1[Flag] = PrevFlag, 0, 1 )
This finds the previous year, if it exists, using a max (in case the years aren't contiguous), then looks up the flag for that particular year, and finally checks if the current flag differs from the previous flag (if there is a previous flag).

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.

DAX - Find and return the category value with the highest summarized measure

In the following example,
Highest Shipping Unit - should be the "ActualArea" where TotalPS has the maximum value.
This is a summarized table grouped by - Customer_ID, ProductDesignation,Package_CD,ActualArea
As you can see above I cannot produce the desired results. The DAX for HighestShippingUnit measure is as follows. I can't seem to figure out what I'm doing wrong.
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
VAR maxps =
MAXX ( tab, [GroupTotalPS] )
RETURN
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
DeviationReport is the name of my table in the dataset.
Please can you tell me what I'm doing wrong in my DAX or how to do it in a more efficient manner?
You were actually very close with your attempt.
If you change the final line from
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
to
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), [ActualArea] )
then it should work.
The reason is that using CALCULATE re-introduces the ActualArea filter context that you removed with ALLEXCEPT so that you only see the current ActualArea.
Thanks to #Alexis Olson. I managed to fix the problem.
The DAX is as follows -
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD],
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
RETURN
MAXX(TOPN(1, tab, [GroupTotalPS]), [ActualArea])
)

DAX subquery measure?

I'm struggling with a DAX query and wondered if you could help?
Consider this table (or visualisation) called 'Builds':
Build....App....Status
Build1...App1...UAT
Build1...App2...Complete
Build2...App1...Complete
Build2...App2...Complete
I would like to add a measure column called 'AppsOutstanding' to show a count of Apps for that Build that aren't 'Complete'. Like so:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....1
Build2...App1...Complete....0
Build2...App2...Complete....0
I almost need to do a 'subquery' measure!? Something like:
SELECT COUNT(Status) FROM Builds
WHERE Build = [The Build In This Row]
AND Status <> 'Complete'
I'm a bit stumped how to translate this into DAX? Here is my unsuccessful attempt:
AppsUnavailable = CALCULATE (
count(Builds[Build]),
CALCULATETABLE (
SUMMARIZE ( Builds,Builds[Status] ),
Builds[Status] <> "Complete"
))
Thanks in advance!
UPDATE
I've tried this, but the count isn't working, and also this DAX filters "Complete" statuses out of my actual results! Which i don't want. I only want to filter the "Complete" statuses out of my count measure....
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
),
FILTER (
ALL ( Builds[Status] ),
Builds[Status] <> "Complete"
)
)
UPDATE 2
I think I'm doing something fundamentally wrong. I've really dumbed it down to just find other 'Builds' with the same name, and it still only returns 1's!
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
)
)
UPDATE 3
This query (when testing on a single table (no joins) sample) produces this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....0
Build1...App2...UAT.........1
Build2...App1...Complete....0
Build2...App2...Complete....0
But i actually need this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........2
Build1...App2...Complete....0
Build1...App2...UAT.........2
Build2...App1...Complete....0
Build2...App2...Complete....0
So Build1 has 2 Apps that are not complete.
I then need to look into why I'm getting all 1's in the 'live' environment. It must be to do with filtering on 2 tables as opposed to 1....
You can replace [The Build In This Row] with SELECTEDVALUE ( Builds[Build] ).
Try this:
AppsUnavailable =
CALCULATE (
COUNT ( Builds[App] ),
FILTER (
ALL ( Builds[Status], Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
&& Builds[Status] <> "Complete"
)
) + 0
powerBI Table visualization internal use SUMMARIZECOLUMNS which is removed row without data from the measure. You have 2 options:
-Right click on GroupBy Column -> "Show item with no data"
-Add +0 to calculation after the last parenthesis
UPDATE:
CALCULATE (
COUNT ( YourTable[App] ),
FILTER (
ALL ( YourTable[App], YourTable[Build], YourTable[Status] ),
YourTable[Build] = SELECTEDVALUE ( YourTable[Build] )
&& YourTable[Status] <> "Complete"
)
) + 0

Previous Day in Power BI

This may be a really simply one but cannot get this to work.
Invitations_Daily =
CALCULATE (
DISTINCTCOUNTNOBLANK ( AppUser[UserId] ),
USERELATIONSHIP ( AppUser[DateInvitedID], 'Date'[DateId] ),
FILTER (
'Date',
'Date'[DateId]
= SELECTEDVALUE ( 'DateSelector'[DateId], MAX ( 'DateSelector'[DateId] ) )
)
) + 0
I have a measure and want the previous days value. When I put in a -1 then ))+0 it gives me 0, instead of the previous day value. Is there something obvious I am missing or is there something else wrong with my data model etc ?

Is there a way to dynamically switch which relationship is active in powerpivot (based on slicer selection)?

I have a fact table (MORT) and a dimension table (GEO) in PowerPivot (2016). There are two joins between the tables with two columns in MORT relating to one column in GEO. We can call one join RES and the other REG. I have a large number of measures and I want to allow the user to quickly change between seeing the measures using the RES and REG relationships - essentially I want to be able to switch the active relationship (referred to as switch in the code). This would probably be based on a detached slicer.
I have tried nesting an if statement and various ways of defining a variable to use in a CALCUATE but either have the error that USERELATIONSHIP can only be used in a calculate (in the case of the if) or that USERELATIONSHIP only accepts a column reference (in the case of the var).
IF STATEMENT
MEASURE:= CALCULATE(COUNT(MORT[ID]), GEO,
IF(LASTNONBLANK(SWITCH,1)="RES",
USERELATIONSHIP(MORT[RES], GEO[AREA]),
USERELATIONSHIP(MORT[REG], GEO[AREA]))
VAR
MEASURE:=
VAR switch = IF(LASTNONBLANK(Switch,1)="RES", MORT[RES], MORT[REG])
RETURN
CALCULATE(COUNT(MORT[ID]), GEO, USERELATIONSHIP(switch, GEO[AREA])
I could create every measure with an if statement at the start to check the value of switch but this creates a lot of duplicate code.
I want some way for the end user to change the active relationship in a measure but ideally without a lot of duplicated code.
How about like this?
MEASURE :=
IF (
LASTNONBLANK ( Switch, 1 ) = "RES",
CALCULATE ( COUNT ( MORT[ID] ), GEO, USERELATIONSHIP ( MORT[RES], GEO[AREA] ) ),
CALCULATE ( COUNT ( MORT[ID] ), GEO, USERELATIONSHIP ( MORT[REG], GEO[AREA] ) )
)
Edit:
In your comment, you said you tried this:
M :=
VAR calc =
FILTER ( MORT, MORT[CAUSE] >= "I" && 'Remake Data'[CAUSE] <= "I9" )
VAR select =
IF (
HASONEVALUE ( 'SWITCH'[Switch] ),
LASTNONBLANK ( 'SWITCH'[Switch], 1 ),
"ERROR"
)
RETURN
IF (
select = "RES",
CALCULATE ( COUNTROWS ( calc ), USERELATIONSHIP ( MORT[RES], GEO[Area] ) ),
IF (
selection = "REG",
CALCULATE ( COUNTROWS ( calc ), USERELATIONSHIP ( MORT[REG], GEO[Area] ) ),
"ERROR"
)
)
There are a few problems with this, but mainly, if you define something as a variable, then it's constant and won't be affected by other things within a CALCULATE. Try the following instead.
Define a new measure:
CountRowsMeasure =
COUNTROWS ( FILTER ( MORT, MORT[CAUSE] >= "I" && 'Remake Data'[CAUSE] <= "I9" ) )
Then use that measure within your M measure:
M =
VAR select = SELECTEDVALUE( 'SWITCH'[Switch], "ERROR" )
RETURN
SWITCH (
select,
"RES", CALCULATE ( [CountRowsMeasure], USERELATIONSHIP ( MORT[RES], GEO[Area] ) ),
"REG", CALCULATE ( [CountRowsMeasure], USERELATIONSHIP ( MORT[REG], GEO[Area] ) ),
"ERROR"
)