DAX: Filter on a filter with countrows - dax

I'm working in a dataset (relations) that tracks ownership of firms.
If a firm has 8 owners, the firm has 8 rows in the dataset, one for each owner. A owner can have multiple firms. I want a calculated column that for row shows how many firms the specific owner owns in that sector. It is something like; for each row, search how many times the owner or owners appears in the relationship database but only count those that has the same industry code like the mother firm. This is what i have so far:
=
CALCULATE (
COUNTROWS ( Relations );
FILTER (
Relations;
Relations[participantnumber] = EARLIER ( Relations[participantnumber] )
);
FILTER ( Relations; Relations[127_industry] = Relations[127_industry] )
)
But this just gives me the total amount of firms an owner is mentioned in regardless of industry code.
Thanks!

Try this:
=
CALCULATE (
COUNTROWS ( Relations );
ALLEXCEPT( Relations; Relations[participantnumber]; Relations[127_industry])
)
How it works: for each row, you need to have access to the entire table so that you can count all relevant relations. However, you want to filter the total count by participant and industry that are current for the row. ALLEXCEPT does that - it allows you see the entire table while preserving current participant and industry.

Related

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

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:

Return the largest category as an extra column or measure in DAX based on calculation and with criteria

I'm a DAX beginner using it in the context of a data model in Excel. I have a large dataset with several tens of thousands of rows split into various groups with record numbers, classes, year, Group name, location, status and lots of other categories of data.
I'd like to add some specific calculated columns for particular key filters such as the location with the largest sales within each Group where the source is still live, and have a measure that works for whichever fields I bring into a pivot (e.g. if I added year, source, state or any combination etc)
For example in the simplified table below I'd like to calculate a column that returns the dominant location for each Group by Sales, subject to only including live records. So for Group A I'd add up all sales for USA and Canada where the Status is live, then take whichever category is largest (Canada for Group A, USA for Group B) and add it as the Dominant location as below:
Group
Location
Status
Sales
Dominant Location
Group A
USA
Live
500
Canada
Group A
USA
Disc
250
Canada
Group A
USA
Disc
290
Canada
Group A
Canada
Live
875
Canada
Group A
Canada
Live
115
Canada
Group A
USA
Live
310
Canada
Group B
USA
Live
310
USA
Group B
UK
Live
285
USA
Group B
UK
Live
705
USA
Group B
USA
Live
430
USA
Group B
USA
Live
670
USA
My original thought process was to use GroupBy to return a table of live sales by location for each group and then use topn to select the largest location by live sales and then SelectColumns to return the location although this doesn't appear to work (or is coded wrongly). Example failed attempt below:
=SELECTCOLUMNS( TOPN(1, GROUPBY('Table',[Location],"Live Sales",SUMX(CURRENTGROUP(),[Sales]* IF([Status]="Live",1,0)) ),[Live GNWP]) ,"Location",[Live GNWP] )
I have managed to add this column with an intermediate calculated column using:
Intermediate=CALCULATE(SUM([Sales]), FILTER('Table', EARLIER([Group]) = [Group] && [Status]="Live" && EARLIER([Location] = [Location])))
Then
=CALCULATE (
FIRSTNONBLANK (
TOPN ( 1,
VALUES ('Table'[Location] ),
CALCULATE ( MAX ( [Intermediate]) )
), 0 ), ALLEXCEPT ( 'Table','Table'[Group] ) )
This does work as a calculated column, but my actual data is too large in row count and field count to be adding extra intermediate calculated fields every time I want a new criteria for selecting the largest category. I also cannot replicate this as a measure so would appreciate understanding why my initial method didn't work and if it's possible to generate this field as a calculated column in one step and then additionally how to get it to work as a measure.
Here's one way to write a corresponding measure without intermediate calculated columns:
Dominant Location =
VAR Summary =
SUMMARIZE (
FILTER ( ALLEXCEPT ( 'Table', 'Table'[Group] ), 'Table'[Status] = "Live" ),
'Table'[Location],
"#SumSales", SUM ( 'Table'[Sales] )
)
RETURN
SELECTCOLUMNS (
TOPN ( 1, Summary, [#SumSales], DESC ),
"Location", 'Table'[Location]
)

A single value for column .... cannot be determined

I have 2 tables for stock management. 1 for the list of stock and some other properties and 1 for the daily values (i have a relationship between both on the index of the stock).
I would like to have a weekly performance ie the value has increased/decreased by xx from the previous week.
So I created a table (weeklies) with a few rows which correspond to a week for each row. I have 2 columns: 1 is the beginning date of the week, 1 is the last date of the week.
Im creating a calculated third column with the sum of all the values at the beginning date of a given week :
CALCULATE (
SUMX ( Daily_Stock; [Price] * RELATED ( Stock_list[Qty] ) );
FILTER ( Daily_Stock; Daily_Stock[Date] = weeklies[begin_date] )
)
It works fine but I would like to exclude some stocks which were sold before the beginning date (i have other reasons to be able to achieve this) so I'm trying to multiply by 0 if it is the case for that specific stock.
CALCULATE (
SUMX (
Daily_Stock;
[Price] * RELATED ( Stock_list[Qty] )
* IF ( RELATED ( Stock_list[sold_date] ) < weeklies[begin date]; 0; 1 )
);
FILTER ( Daily_Stock; Daily_Stock[Date] = weeklies[begin_date] )
)
There I have the following error :
A single value for column sold_date in table Stock_list cannot be determined.
Tweaking around a little bit and I had the same error on the weeklies table.
Does anyone know what I should be doing here?
I can explain more, I wanted to avoid a too-long post.
thanks
I think the issue is the relation.
I assume the RELATED is based on the stock index you mentioned.
I think related stock_list[sold_date] returns all dates that RELATED stockID has ever been sold.
Which would mean you are trying to compare more than one date with weeklies[begin date].
image copied from powerpivotpro on using VALUES with IF in measures.
If i am right, you need another way of relating to your stocklist to get singular matches. I am not sure if the VALUES solution rob collie uses for measures will work here, but maybe it is worth testing. Rob collie powerpivotpro - Magic of IF(VALUES)

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).

How to combine 6 tables in one Matrix, show top 12 and categorize the rest as others?

I need to be able to sum availability based on product and say show me top 3, and categorize the rest as Others. I have two tables in a matrix connected by a product table.
I tried so many ways -
i was able to create this measure for July (which is what i will be sorting with) - I get the correct ranking column for July.
i know i'm missing something. i tried to take that ranking measure statement and add an if statement and couldn't get it to do the ranking.
the picture would make more sense *(my formulas are based on actual column names)
Partner Ranking =
VAR summry =
SUMMARIZE (
ALLSELECTED ( Latest ),
[partner_group],
"Sum", COUNT ( Latest[site_url] )
)
VAR tmp =
ADDCOLUMNS ( summry, "RNK", RANKX ( summry, [Sum],, DESC, DENSE ) )
RETURN
MAXX (
FILTER ( tmp, [partner_group] = SELECTEDVALUE ( Latest[partner_group] ) ),
[RNK]
)
I don't know what to do next. how can i do this when i have a separate table that is the product name that links the two tables?

Resources