Ok, highly simplified table of three columns, order#, product#, and quantity...
Order | Product | Qty
1 | A | 10
1 | B | 20
2 | C | 30
I want to calculate an average of quantity, so.. this is at the "default grain":
AvgQty = 60/3 = 20
Easy, however, i also then want to remove Product:
Order | Qty
1 | 30
2 | 30
and now the Qty should re-aggregate [as they would with a sum()], and now I would want AvgQty to return the average of these new lines...
AvgQty = 60/2 = 30
If tried to do this by grouping by Order explicitly like so:
measure :=
IF (
ISFILTERED ( 'Table'[Product] ),
AVERAGEX (
SUMMARIZE (
'Table',
'Table'[Order],
'Table'[Product],
"SumQty", SUM ( 'Table'[Qty] )
),
[SumQty]
),
AVERAGEX (
SUMMARIZE (
'Table',
'Table'[Order],
"SumQty", SUM ( 'Table'[Qty] ) ),
[SumQty]
)
)
It doesn't quite work due to the total of the column technically not being filtered by product, so it continues to still show the incorrect total...
I am not certain how to override this..?
My actual calc is not just a simple average, but the main problem I am facing is ensuring I can get a 'recalculation' of the Qty at a new grain.. if I can nail this, I can fix my own problem.. the solution could well be to also load the table to the model at the order grain too!!! ;)
I also thought about it the last days and I am afraid there is no way to solve this for the following reasons:
there is no function in DAX to return the whole table that was calculated as your rows
there is no function to tell you what was aggregated there
for a single row you could find out what was filtered using complex cascading ISFILTERED functions but this is not really feasible nor reliable
the biggest problem: when you are on the total or sub-total level, there is no way to find out what was used for the detail rows as none of the existing functions like ISFILTERED, HASONEVALUE, etc. would work
so for DAX this cannot be solved at the moment from my point of view
in case you are using MDX to query your model (e.g. a Pivot Table) you could create a MDX measure which uses the AXIS()-function to return the set which was used on rows/columns and is it in a COUNT() function
Related
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 table that is similar to the data set I am working on (although much simpler) is below that I would like to calculate some measures on and then find the percentiles of the measures.
Table Name: Data
Owner AgeRating OtherRating
A 1 2
A 4 4
A 4 6
B 3 3
B 3 9
B 7 4
C 8 8
C 4 2
First - A little background: I start by taking an average of the ratings (By Owner) and then normalize all ratings by dividing each rating by the maximum owner's rating - This creates the measure I would like to take the percentile of:
NormAgeRating=
average(Data[AgeRating])/
calculate(
maxx(
SUMMARIZE(Data,[Owner],"avg",average([AgeRating]))
,[avg]
)
,all(Data[owner])
)
I have a pivot table with Rows being the owner which then looks like
Owner NormAgeRating
A .5
B .72
C 1
Now for the question:
I would like to get the .33 percentile.inc of the new NormAgeRating. I would like to use this to classify each owner into groups (<=33%ile or > 33%ile)
This is what I am trying to get to:
Owner NormAgeRating 33%ile classification
A .5 .64 bottom
B .72 .64 top
C 1 .64 top
I have tried this with no success and many other variation with different groupby's etc. and continually get the wrong value:
33%ile=percentilex.inc(all(data[owner]),[NormAgeRating],0.33)
Any help would be greatly appreciated
Update:
When I try sumx countx and averagex in the form:
=
averagex(
SUMMARIZE(
all(Data[Owner]),
[Owner],
"risk",[NormAgeRating]),
[risk]
)
I am getting the right values, so I am not sure why using percentilex.inc/exc would produce the wrong values...
PERCENTILEX (and all iterator functions) operates row by row on the table in the first argument. Therefore, you need that table to be at the desired granularity before you try to compute the percentile, which means you need to summarize Data[Owner] so that you have a unique row per owner rather than iterating over the raw column.
Keeping this in mind, both measures can be written similarly:
NormAgeRating =
DIVIDE (
AVERAGE ( Data[AgeRating] ),
MAXX (
SUMMARIZE (
ALL ( Data[Owner] ),
Data[Owner],
"Avg", AVERAGE ( Data[AgeRating] )
),
[Avg]
)
)
33%ile =
PERCENTILEX.INC (
SUMMARIZE (
ALL ( Data[Owner] ),
Data[Owner],
"Risk", [NormAgeRating]
),
[Risk],
0.33
)
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).
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?
I'm trying to figure out how to build a measure that sums a total, but only taking the first non-empty row for a user.
For example, my data looks like the below:
date user value
-----------------
1/1/17 a 15
2/1/17 a 12
1/1/17 b null
5/1/17 b 3
I'd therefore like a result of 18 (15 + 3).
I'm thinking that using FIRSTNONBLANK would help, but it only takes a single column, I'm not sure how to give it the grouping - perhaps some sort of windowing is required.
I've tried the below, but am struggling to work out what the correct syntax is
groupby(
GROUPBY (
myTable,
myTable[user],
“Total”, SUMX(CURRENTGrOUP(), FIRSTNONBLANK( [value],1 ))
),
sum([total])
)
I didn't have much luck getting FIRSTNONBLANK or GROUPBY to work exactly how I wanted, but I think I found something that works:
SUMX(
ADDCOLUMNS(
ADDCOLUMNS(VALUES(myTable[User]),
"FirstDate",
CALCULATE(MIN(myTable[Date]),
NOT(ISBLANK(myTable[Value])))),
"FirstValue",
CALCULATE(SUM(myTable[Value]),
FILTER(myTable, myTable[Date] = [FirstDate]))),
[FirstValue])
The inner ADDCOLUMNS calculates the first non-blank date values for each user in the filter context.
The next ADDCOLUMNS, takes that table of users and first dates and for each user sums each [value] that occurred on each respective date.
The outer SUMX takes that resulting table and totals all of the values of [FirstValue].