In DAX, is it possible to check if value exists in another table using measure instead of calculated column? - dax

I'm hoping to create a measure of distinct count of a customer column, on the condition if customers in this column does not exist in another table's customer column.
I know I can create a calculated column checking if the customer exists, and then use the calculate function filtering out those who do exist. But is it possible to achieve this without creating the calculated column?
Please note this is in Power Pivot, not Power BI so I can't really use 'treatas' or 'in'. Thanks a lot.

Assuming tables named Table1 and Table2:
MyMeasure :=
VAR T2Customer =
VALUES( Table2[Customer] )
RETURN
CALCULATE(
DISTINCTCOUNT( Table1[Customer] ),
NOT (
CONTAINSROW(
T2Customer,
Table1[Customer]
)
)
)

Yes, You can achieve it using EXCEPT()function:
Let's say that we have 2 tables like this:
Customer_Table1:
Customer_Table2:
Now we can use this measure to achieve our result:
CountOfDistinctCusts =
COUNTROWS (
EXCEPT (
VALUES ( Customer_Table1[Customer] ),
VALUES ( Customer_Table2[Customer] )
)
)
If we test the code:

Related

Powerbi groupby filter for not seeing the data if it contains a specifiek value

iam trying to create a measure with a filter.
i have fields TPLNR and TXT04. A TPLNR contains double/triple records with different TXT04 values.
TPLNR 4OR-TTE-A-TY09159 have 3 different values in TXT04. iam trying to create a measure that if a TPLNR has the value DLFL i dont want to see the other 2 values also in my output. in this case i dont want to see the TPLNR 4OR-TTE-A-TY09159 in my visual and table drill through. is it possible to create such measure?
enter image description here
could someone help me with this?
In order to do so, I suggest you use the filter functionality offered by PowerBI, in this way you won't need a measure just to filter your data.
In order to make filtering easier, I suggest you use a flag to specify what to include or exclude, you can calculate it in 3 ways.
in your source, so it's already ready to use in PowerBI
Using M (Power Query)
Reference the existing table (which makes a sort of copy of it)
filter it to include only the rows you want to include or exclude
join with the original table and create you "flag" by managing missing values (a sort of COALESCE)
specify that this "clone" table must not be loaded into the model
Using DAX calculated column
The formula might look like the one below
MyFilterFlag =
CALCULATE (
CONTAINSROW ( VALUES ( 'Table'[TXT04] ), "DLFL" ),
ALLEXCEPT ( 'Table', 'Table'[TPLNR] )
)
The formula checks for the available values in the column TXT04, by keeping as a filter only TPLNR, in practice it behaves as a group by
If you need more conditions just add them to the checked expression (&& = AND)
CALCULATE (
CONTAINSROW ( VALUES ( 'Table'[TXT04] ), "DLFL" )
&& CONTAINSROW ( VALUES ( 'Table'[INACT] ), "X" )
,ALLEXCEPT ( 'Table', 'Table'[TPLNR] )
)

PowerPivot Count Duplicates

would appreciate any help with this...
I have a table of Invoice Data, Including Company Number and Date, I need to count the number of duplicates where a company has more than one invoice for the same day. ( I need to count in PowerPivot itself, as I will be performing more calculations based on the results, so wouldn't want to drag into a pivot table at this stage )
Thanks in advance for your help.
Kind Regards
Gavin
Please find the DAX:
Count :=
CALCULATE (
COUNTROWS ( Table1 ),
ALLEXCEPT ( Table1, Table1[Company], Table1[Date] )
)
Thanks

A way to filter a distinct set of columns using a measure from the same table - Tabular2017

Overview of the table in question
I need to get a distinct count of the column Fkey_Dim_Resource_ID that has holiday to spare.
My Table consists of five columns:
Resource_Allocated_Holiday_ID (Primary Key)
Fkey_Dim_Resource_ID
Fkey_Dim_HolidayYear_ID
Fkey_Dim_Company_ID
Allocated_Holiday_Hrs_Qty
Measure:
Allocated Holiday (Hrs):= Var X= SUM([Allocated_Holiday_Hrs_Qty])
Return if(X =0; BLANK();X)
This measure below then uses the above, and the holiday spent from another metric:
Remaining Holiday (Hrs):= Var X = 'HolidayEntry Numbers'[Allocated Holiday (Hrs)] - [#Holiday Hours]
Return if(X=0;BLANK();X)
And now, I would like a metric that gives me the distinct count of Fkey_Dim_ResourceID where 'Remaining Holiday (hrs)' >0.
I have tried a lot of different stuff, but cannot seem to get it right.
test:=
ADDCOLUMNS(
SUMMARIZE('HolidayEntry Numbers'
;'HolidayEntry Numbers'[Fkey_Dim_Company_ID]
;'HolidayEntry Numbers'[Fkey_Dim_Resource_ID];
'HolidayEntry Numbers'[Fkey_Dim_HolidayYear_Id]
)
;"RemainingHoliday"; sum( [Remaining Holiday (Hrs)])
)
I would like for a distinct count of Fkey_Dim_Resource_ID that has holiday left, that takes into account the context.
Thanks in advance.
With this measure:
test4 virker når ressourcen er med:=COUNTROWS (
FILTER (
ADDCOLUMNS (
VALUES ( 'HolidayEntry
Numbers'[Fkey_Dim_Resource_ID]);
"remholiday"; CALCULATE ( [Remaining Holiday
(Hrs)] )
);
[remholiday] > 0
)
)
I get the following result:
Result of the advice1
So the metric works, when in the context of a Resource, but not when in the context of a Fkey_dim_holiday_Year_ID.
Thanks ion advance.
Resources with remaining holiday hours =
COUNTROWS ( // counts rows in a table
FILTER ( // returns a table, filtering based on predicate
// below is unique values of the column in context, as a
// one-column table
VALUES ( 'HolidayEntry Numbers'[Fkey_Dim_Resource_ID] ),
[Remaining Holiday (hrs)] > 0 // keep rows meeting this criterion
)
)
As a matter of style, you should fully qualify column names as 'Table'[Column], and never fully qualify measure references, i.e. don't prefix with table name. This conforms with all style guides I know, and helps to ensure your code is unambiguous (since both columns and measures are referenced in square brackets).

Retrieving a maximum value from a SUMMARIZECOLUMNS table

I have a query and the following results, executed from DAX Studio:
What I would like to do now is to expand the query so that I can retrieve maximum Total Sales from the table that SUMMARIZECOLUMNS produces. For example, based on the rows displayed in the results, I'd like a way to return 10234.35. Is there a way to do this?
Wrap the whole SUMMARIZECOLUMNS part in a MAXX.
MAXX(
SUMMARIZECOLUMNS([...]),
[Total Sales]
)
The MAXX(<table>,<expression>) function iterates through each row of the <table> from its first argument taking the maximum value of the <expression> in the second argument.
As #greggyb points out, a more efficient implementation would be
CALCULATE (
MAXX ( VALUES ( Customers[Customer Key] ), [Sales Amount] ),
FILTER ( Products, Products[Product Name] = "Fabrikam Laptop12v M2080 Silver" ),
FILTER ( 'Calendar', 'Calendar'[Calendary Year] = 2008 )
)
since this doesn't require creating the whole summary table in memory.

DAX - Lookup value to retrieve exchange rate

In table "Paypal", I have:
And in table "Câmbios":
And now, I'm adding a calculated column to "Paypal" table with the formula:
Câmbio = LOOKUPVALUE('Câmbios'[Câmbio];'Câmbios'[Mês];MONTH('Paypal'[Date]))
Which is returning the error:
A table of multiple values was supplied where a single value was expected.
This doesn't make sense to me.
Can anyone help?
Thanks
The problem is Câmbios table contains repeated values for at least one month and the LOOKUPVALUE function doesn't know which value use to retrieve the specified column.
You can use instead:
Cambio =
CALCULATE (
MAX ( Cambio[Cambio] ),
FILTER ( Cambio, [Mes] = MONTH ( EARLIER ( Paypal[Date] ) ) )
)
Or delete the repeated values from Cambios[Mes].

Resources