How to count two column values - dax

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.

Related

Grand Total is incorrect is SSAS Tabular Model

The number I see in the grand total is different from the sum of the rows (for previous year column [Önceki Yıl IPP]).
Table columns
Measures:
IPP Ciro:=SUM([Amount_])/1000
This measure calculates sum of revenue(Amount_)
Önceki Yıl IPP:=
VAR LastDaySelectionIPP =
LASTNONBLANK ( Tarih[Tarih], [IPP Ciro] )
VAR CurrentRangeIPP =
DATESBETWEEN ( Tarih[Tarih], MIN ( Tarih[Tarih] ), LastDaySelectionIPP )
VAR PreviousRange =
SAMEPERIODLASTYEAR ( CurrentRangeIPP )
RETURN
IF (
LastDaySelectionIPP >= MIN ( Tarih[Tarih] ),
CALCULATE ( [IPP Ciro], PreviousRange )
)
This measure calculates sum of previous year revenue.
I want to compare the current year with the last year same period.
Why is the Grand total is 98.998 for previous year[Önceki Yıl IPP]?
Grand Total is incorrect is SSAS Tabular Model
It’s not jumping out at me, but here’s how to debug it.
Add a new measure like this and add it to your table:
Debug:=
VAR LastDaySelectionIPP =
LASTNONBLANK ( Tarih[Tarih], [IPP Ciro] )
VAR MinDate = MIN ( Tarih[Tarih] )
VAR CurrentRangeIPP =
DATESBETWEEN ( Tarih[Tarih], MIN ( Tarih[Tarih] ), LastDaySelectionIPP )
VAR PreviousRange =
SAMEPERIODLASTYEAR ( CurrentRangeIPP )
VAR WrongAnswer =
IF (
LastDaySelectionIPP >= MIN ( Tarih[Tarih] ),
CALCULATE ( [IPP Ciro], PreviousRange )
)
RETURN LastDaySelectionIPP & “,” & MinDate & “,” & [IPP Ciro] & “,” & WrongAnswer
It will list the values you are using in the calculation and I predict you’ll find the unexpected value. Update us on what you find!

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