How to perform a column calculation in a CALCULATE() - dax

My table looks like this:
Doc Code
A 61
A 61X
B 62
B 61
C 61
C 62
C 62X
I am trying to create a column in DAX that just checks whether a Code contains an X by Doc. So the result should look like this:
Doc Code DAXColumn
A 61 TRUE
A 61X TRUE
B 62 FALSE
B 61 FALSE
C 61 TRUE
C 62 TRUE
C 62X TRUE
I want to do this:
DAXColumn =
VAR currentDoc = Doc
RETURN
CALCULATE(IF(ISERROR(SEARCH("X",Doc)) = TRUE, FALSE, TRUE),
FILTER(myTable,Doc = currentDoc))
It looks like I can't pass a column into the CALCULATE parameter. How could I get the results I am looking for? Thanks in advance.

Column =
VAR _1 =
SUMMARIZE (
FILTER ( _fact, CONTAINSSTRING ( _fact[Code], "X" ) ),
[Doc],
[Code]
)
VAR _2 =
CALCULATE ( MAXX ( FILTER ( _1, [Doc] = MAX ( _fact[Doc] ) ), [Code] ) )
VAR _3 =
SWITCH ( TRUE (), ISBLANK ( _2 ) = TRUE (), FALSE (), TRUE () )
RETURN
_3

Here is my suggestion - I find this code a bit more readable so it's easier to understand at a glance what is going on.
Contains X :=
VAR _codes =
CALCULATETABLE (
VALUES ( 'Table'[Code] ),
ALLEXCEPT ( 'Table' , 'Table'[Doc] )
)
VAR _filter =
FILTER (
_codes ,
CONTAINSSTRING ( [Code] , "X" )
)
VAR _count = COUNTROWS ( _filter )
RETURN IF ( _count > 0 , TRUE , FALSE )
The logic:
Calculate a single column table with all values of Code for the current (row context) value of Doc
Apply a filter over the codes table looking for the desired string
Count the rows in the remaining table
If the count is zero, the _count variable will evaluate to blank. For a non-zero count we want to return TRUE.

Related

How to count two column values

I want count two columns using power bi to create a visual.my measure as below
Test Measure = COUNTA('Export'[Line])+COUNTA('Export'[Line 2]),
You need to create a calculated table with all the lines values, like this
TableValues =
DISTINCT ( UNION ( DISTINCT ( 'Table'[Line] ), DISTINCT ( 'Table'[Line_2] ) ) )
With that table done, you can write the following measure
CountValues =
VAR _CurrentValue =
SELECTEDVALUE ( TableValues[Line] )
VAR _C1 =
CALCULATE ( COUNTA ( 'Table'[Line] ), 'Table'[Line] = _CurrentValue )
VAR _C2 =
CALCULATE ( COUNTA ( 'Table'[Line_2] ), 'Table'[Line_2] = _CurrentValue )
RETURN
_C1 + _C2
The calculations count all the instances of a specific Line. It's important that the TableValues doesn't have any relationship with the other tables.
Output
Using 'TableValues'[Line] as the Line and CountValues as the metric.

Problem in using filter context in CALCULATE

In PowerPivot DataModel I have written following DAX Code:
=VAR var1 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
ALL ( 'CapitalPrepayment'[EffectiveYear] )
)
VAR var2 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[EffectiveYear] = "1399",
'CapitalPrepayment'[Subject] = "Invoice"
)
VAR var3 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
'CapitalPrepayment'[Subject] = "Proforma",
'CapitalPrepayment'[EffectiveYear] = "1398"
)
VAR var5 =
CALCULATE (
SUMX ( 'CapitalPrepayment', 'CapitalPrepayment'[Amount] ),
ALL ( 'CapitalPrepayment'[Subject] )
)
RETURN
IF ( Var5 < 0, 0, IF ( Var5 = 0, 0, IF ( VAR1 + VAR2 <= 0, Var3, 0 ) ) )
and the result is:
As you can see, the problem is that grand total of [Var5] for some vendors and grand total for [Final] for all vendors is highly related to filters come from EffectiveYear ( at least Ithink so ), so the result would be zero. in addition, changing the filter context from visual ( for example removing [vendor] and [EffectiveYear] ) will disrupt the result. how can I fix this?
In my written DAX, [Var1]~[Var3] do include [subject] and [EffectiveYear] as filter arguments, but [Var5] does not for [EffectiveYear] . so I expect that the problem arise from [EffectiveYear].
I tried to include [EfectiveYear] in someway in CALCULATE but it didn't work.

Based on a value show different message

Follow Up :
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 (text - will explain)
The second table is CustomerLimit which is basically consecutive numbers between 1 and 100.
Used the tier measure as the answer below (thank you)
Tier =
VAR Limit = SELECTEDVALUE ( CustomerLimit[CustomerLimit] )
VAR CustCount = COUNT ( Customers[CustomerID] )
RETURN
IF (
ISBLANK ( Limit ), "Select a value",
IF ( CustCount > Limit, "Good", "Bad" )
)
Now I need to aggregate 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.
Is that possible ?
You can capture the limit using SELECTEDVALUE and then compare.
Tier =
VAR Limit = SELECTEDVALUE ( CustomerLimit[CustomerLimit] )
VAR CustCount = COUNT ( Customers[CustomerID] )
RETURN
IF (
ISBLANK ( Limit ), "Select a value",
IF ( CustCount > Limit, "Good", "Bad" )
)

Count unique matching items with filter as a calculated column

I have two tables are Data and Report.
Data Table:
In Data table contain three columns are Item, status, and filter.
The item 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 filter column contains two different filters which are A1 and A2.
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay". I am looking for count against filter A1 and A2 according to the comments.
I would like to create a new calculated column in the report table in order to get the unique count according to the comments and filter based on the data table columns item and status.
DATA:
REPORT
Alexis Olson helped the following calculated column in order to get the unique count. I am trying to add one more filter in existing DAX calculated column but it's not working. Can you please advise?
1.Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
2.Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
3.Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)
I think you can just add a filter to CALCULATE:
Filter by A1 Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment,
DATA[FILTER] = "A1"
)
For the second method,
Filter by A1 Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] && REPORT[FILTER] = "A1" ),
DATA[ITEM]
)
)
I do not recommend using the third one but it would be like this
Filter by A1 Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF (
CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS]
&& CALCULATE ( SELECTEDVALUE ( DATA[FILTER] ) ) = "A1",
1,
0
)
)

How Crossjoin two tables in Dax created from Generateseries?

I want to crossjoin to "table" created with GenererateSeries in a single Expression
Table =
VAR TableAnnee =
GENERATESERIES ( 2010; 2020; 1 )
VAR TableMois =
GENERATESERIES ( 1; 12; 1 )
RETURN
CROSSJOIN ( TableAnnee; TableMois )
Then I have an error message:
"CROSSJOIN function does not authorize two columns with the same name: [Value]."
How can I crossjoin TableAnnee and TableMois without creating two real tables but in one expression in that table?
You can use the SELECTCOLUMNS function to give your generated tables appropriate column names. The default column name is [Value], so you need to rename that default to do the crossjoin.
Table =
VAR TableAnnee =
SELECTCOLUMNS ( GENERATESERIES ( 2010; 2020 ); "Year"; [Value] )
VAR TableMois =
SELECTCOLUMNS ( GENERATESERIES ( 1; 12 ); "Month"; [Value] )
RETURN
CROSSJOIN ( TableAnnee; TableMois )

Resources