Filtering by secondary field in DAX - powerbi

I'm new to DAX.
My model contains a single table called Notices. Notices has 235,969 rows.
Notices has fields CustomerID, NoticeNo and NoticeStatus.
When I set the filter context to 'CANCL' Notices[NoticeStatus], I can see that there are 3 notices that have a CANCL status.
As such, the measure below evaluates to 3 since each of the remaining notices belong to 3 separate customers. However, I would like to base the aggregate on the unfiltered table, but filter out the rows (after aggregating) based on the CustomerIDs that remain in the filter context and [ObCount] = 1. In this case, the measure needs to evaluate to 0 or BLANK(), as none of the CustomerIDs in the filter context remain after filtering for [ObCount] = 1.
Customers with Single Notice Only =
COUNTROWS (
FILTER (
SUMMARIZECOLUMNS (
Notices[CustomerID],
Notices,
"ObCount", [All Notices Outstanding]
),
[ObCount] = 1
)
)
[All Notices Outstanding] = COUNTROWS(Notices)

You should be able to do this by applying the filtered table as a table filter argument to Notices using CALCULATE:
Customers with Single Notice Only =
CALCULATE (
COUNTROWS ( Notices ),
FILTER (
SUMMARIZECOLUMNS (
Notices[CustomerID],
ALL ( Notices ),
"ObCount", [All Notices Outstanding]
),
[ObCount] = 1
)
)
Note the use of ALL to remove the filter status filter when summarizing.

Related

error in Calculate : A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed

I have these two tables that are mutually exclusive (not connected in any way) .
The first table has date , number of customers on the dayDISTINCTCOUNT(sales[user_name]), total sales , tier (calculated measure below )
The second table is CustomerLimit which is basically consecutive numbers between 1 and 100.
Tier = VAR Limit = SELECTEDVALUE ( CustomerLimit[CustomerLimit] )
VAR CustCount = COUNT ( sales[user_name] )
RETURN
IF (
ISBLANK ( Limit ), "Select a value",
IF ( CustCount > Limit, "Good", "Bad" )
)
Now I need to aggregate YTD the total amount of customers by Tier. I used calculate(DISTINCTCOUNT(sales[user_name]),Tier = "Good") .
It give me an error of : A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.
Can someone please help me with how can I adjust this calculate function to aggregate them?
Thank you
You cannot use a measure value in a predicate within a calculate filter.
But you can create a filter using FILTER to filter your table by your measure value. Your measure must be on the form of :
Good Customers =
CALCULATE (
DISTINCTCOUNT ( 'sales'[user_name] ) ,
FILTER (
'sales' ,
[Tier] = "Good"
)
)

DAX Service recall rate measure

I'm struggling to write/calculate this measure in DAX. The definition of recall rate is count of repeat service bookings (count of distinct booking number, should be distinct anyway but just in case) for a customer, asset combination within a week (Closed on date, 7 day period). So I go out to fix a machine, if I get called out again to fix the same machine for the customer within a week that is then a recall of 1 (or more if i get called out multiple times within a week). I've highlighted the groups in different colours. Null Assets, Closed On and null booking number needs to be filtered out (this is done by inner join in SQL in below code, needs to be in DAX query) Thanks! EDIT : Sorry realised it would be more helpful if I posted sql code to generate data please see below :
SELECT
FB.BookingNumber,
FB.EngineerEmployeeID,
FWO.ServiceAccountRecID AS Customer,
FWO.AssetRecID AS Asset,
FWO.ClosedOn
FROM dbo.FactWorkOrder AS FWO JOIN dbo.FactBooking AS FB ON FB.WorkOrderID = FWO.WorkOrderID
WHERE FWO.WorkOrderType = 'Breakdown'
AND AssetRecID IS NOT NULL
AND ClosedOn IS NOT NULL
ORDER BY BookingNumber
It's most efficient if you first define a calculated column that gives the first CloseOn date for each Customer/Asset combination.
FirstClosed =
CALCULATE (
MIN ( WorkOrder[ClosedOn] ),
ALLEXCEPT ( WorkOrder, WorkOrder[Customer], WorkOrder[Asset] )
)
and then write a measure
TotalRecalls =
COUNTROWS (
FILTER (
WorkOrder,
WorkOrder[ClosedOn] > WorkOrder[FirstClosed] &&
WorkOrder[ClosedOn] < WorkOrder[FirstClosed] + 7
)
)
However, you can do this all within a single measure if you prefer.
TotalRecalls =
VAR AddCol =
ADDCOLUMNS (
WorkOrder,
"#FirstClosed",
CALCULATE (
MIN ( WorkOrder[ClosedOn] ),
ALLEXCEPT ( WorkOrder, WorkOrder[Customer], WorkOrder[Asset] )
)
)
RETURN
COUNTROWS (
FILTER (
AddCol,
WorkOrder[ClosedOn] > [#FirstClosed] &&
WorkOrder[ClosedOn] < [#FirstClosed] + 7
)
)
Either way, here's what this looks like used in a visual:
I would first create a "Booking Key" column:
Booking Key = [Customer] & "|" & [Asset] & "|" & WEEKNUM ( [ClosedOn] )
Then I would create a Measure to return a modified distinct count on the Booking Key:
# Repeat Service Bookings =
VAR v_Count = DISTINCTCOUNT ( 'Table'[Booking Key] )
RETURN IF ( v_Count > 1, v_Count - 1 )
I would add # Repeat Service Bookings to the Visual Level Filters of your table visual, with the filter set to: is greater than 1.

Count unique matching items as a calculated column

I have two tables are Data and Report.
Data Table:
In Data table contain two columns are Item and status.
The item column contains duplicated entry and the item column contains text and number or number only or text only.
The status column contains two different text/comments, "Okay" and "Not Okay"
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay".
I would like to create a new calculated column in the report table in order to get the unique count according to the comments based on the data table columns item and status.
In Excel, I am applying the following formula
F2=SUM((FREQUENCY(MATCH(A$2:$A$19&"",$A$1:$A$19&"",0)*($B$2:$B$19=$D3),ROW($A$2:$A$19))>0)+0)-1
in order to get my final result.
I don't want measure solutions.
DATA TABLE:
REPORT TABLE:
EXCEL LOGIC:
This is much easier in DAX than in Excel and there are many ways to do it.
Here are some possibilities with different approaches:
Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)

RANKX does not give the correct result

I need to create a measure (not column) that would rank users based on value.
Here I am trying to use RANKX function:
ranking =
RANKX(
ADDCOLUMNS(
SUMMARIZE(dim_User,
dim_User[UserID],
dim_User[FirstName]
),
"Ttl Trans", [Ttl Transactions]
)
, [Ttl Transactions]
)
Same result using:
rating2 =
RANKX(
SUMMARIZE(dim_User,
dim_User[FirstName]
), [Ttl Transactions]
)
Tried this way:
rating1 =
RANKX(
SUMMARIZE(dim_User,
dim_User[FirstName],
"Trans",[Ttl Transactions]
), [Trans]
)
But gives me an error:
The value for 'Trans' cannot be determined. Either the column doesn't exist, or there is no current row for this column.
I also tried using COUNTROWS() function, but also no success.
It seems like it works if I use a table, not a table expression.
What am I missing here?
UPDATE:
Adding ALL(dim_User) still giving me 1
ranking =
RANKX(
ADDCOLUMNS(
SUMMARIZE(ALL(dim_User),
dim_User[UserID],
dim_User[FirstName]
),
"Ttl Trans", [Ttl Transactions]
)
, [Ttl Transactions]
)
UPDATE
No matter what user I select, in a rating in a card is always 1.
When you write a measure, it is evaluated within its local filter context. In particular, dim_User is filtered by FirstName within your measure and is, therefore, ranking Ttl Trans only compared to other users with the same FirstName.
Since you are ranking based on user, you want to remove the filter context introduced by the visual in order to get the ranking you expect. I'd suggest replacing the table argument dim_User with ALL(dim_User) to remove all filtering on that table or -- more likely the option you want -- with ALLSELECTED(dim_User) to remove the local filter context introduced by the visual while maintaining any filter context from slicers or page-level filters.
For your card, you'll need to simulate the filter context that exists in the table:
UserRanking =
VAR SelectedUser = SELECTEDVALUE ( dim_User[Name_FirstLast] )
RETURN
CALCULATE (
RANKX ( ALL ( dim_User ), [Ttl Transactions] ),
dim_User[Name_FirstLast] = SelectedUser
)

Filtered Running Total

I am trying to get a running total of a count value in a table, but I need to have the filters I created to apply.
Basically I have 2 tables.
Table one is a user table which contains the user email (useremail) address and the user country (usercountry)
Table two contains the user email (useremail) and a date when they signed an agreement (Created)
I created a measure like this, which gets the user email from the signup table and gives me the running total, but when I apply a filter to the report, the sign up count does not change. It completely ignores the filter
Sign ups =
CALCULATE (
DISTINCTCOUNT( 'SignUps'[useremail] ),
FILTER (
ALLselected ( 'SignUps' ),
'SignUps'[Created] <= MAX ( 'SignUps'[Created] )
)
)
There is a relationship between the two tables where users(useremail) matches sign ups(useremail) One to One and cross filter both directions
Basically the chart below never changes regardless of the usercountry I filter by
Sign Up Chart.
Any idea how I can do this?
You won't need ALLSELECTED unless you are showing visual total.
Remove ALLSELECTED and just pass a table so it can be filtered using your slicer.
Sign ups =
CALCULATE (
DISTINCTCOUNT ( 'SignUps'[useremail] ),
FILTER (
'SignUps',
'SignUps'[Created] <= MAX ( 'SignUps'[Created] )
)
)
Read ALLSELECTED