I am trying to get a distinct count of the vendor keys for each partner but for some reason every time I bring in the vendorkey column from the fact table it returns 1 for measures, when I remove the vendorkey column it returns the accurate count. Is there something I'm missing?
DAX:
testMeasure = CALCULATE(DISTINCTCOUNT(fact_table[vendor_key]),ALLEXCEPT(dim_partner,dim_partner[partner_name]))
Current Output With Vendor Key:
Current Output Without Vendor Key:
Expected Output:
Partner VendorKey testMeasure
Absolutely Pc 4341 1
Affinitas 4341 1
Birak 4341 1
Blue Shield IT Ltd 4316 3
Blue Shield IT Ltd 4336 3
Blue Shield IT Ltd 4341 3
Note: There is already a one to many relationship between the partner table and fact table
Please test this, and let me know:
testMeasure =
CALCULATE (
DISTINCTCOUNT ( fact_table[vendor_key] ),
ALLEXCEPT ( fact_table, dim_partner[partner_name] )
)
Also, DistincCount() is a problematic function, and causes you lots of trouble with your measure if your data grows. change your measure with this. Preference is up to you!
testMeasure =
CALCULATE (
SUMX ( VALUES ( fact_table[vendor_key] ), 1 ),
ALLEXCEPT ( fact_table, dim_partner[partner_name] )
)
Related
I'm working with claims where I use different key joins/sheets to get the whole truth.
Right now I got two different sheets with costs for each item (Price Per Unit), depending on where the repair occur. One for Asia and one for the rest of the world (WW)
Sheet 1: PN-List Asia and Sheet 2: PN-List WW
Inside the mentioned sheets there is a priority (1 - 3) of which supplier I should get prices from.
Column Prio.
I've made a column that either says Asia or WW for each job ID. Regress List [Continent].
Two problem has occurred, total cost for each ID is only the max value (most expensive item). See picture:
The total cost should be: 840,96 but it says 622,10 now.
The second problem is that my grand total is completely wrong, should be a lot more. See below picture:
The formula I've used:
Price Per unit =
IF (
SELECTEDVALUE ( 'Regress List'[Continent] ) = "Asia",
CALCULATE (
MAX ( 'PN-List Asia'[Price Per Unit] ),
FILTER ( 'PN-List Asia', 'PN-List Asia'[Prio] = MIN ( 'PN-List Asia'[Prio] ) )
),
CALCULATE (
MAX ( 'PN-List WW'[Price Per Unit] ),
FILTER ( 'PN-List WW', 'PN-List WW'[Prio] = MIN ( 'PN-List WW'[Prio] ) )
)
)
I just made a new measure:
Price Per Units = SUMX('Regress List', 'Regress List'[Qty] * [Price Per unit])
And it gave me the right answer
Thanks to Ozan Sen for leading me in the right way with Sumx
So, I have the following tables in my Power BI :
Sales : Date | ID_Client | ID_Product | Amount
Client : ID_Client | Name_Client
I would like to get the number of unique BIG clients in any given month. I therefore use the following formula (which I then put in a column in a table with months in rows):
# BIG Clients =
VAR threshold = 10000
RETURN
(
CALCULATE(
DISTINCTCOUNT( Sales[ID_Client] ),
FILTER(
SUMMARIZE(
Sales,
Sales[ID_Client],
"Sales", SUM( Sales[Amount] )
),
[Sales] >= threshold
)
)
)
QUESTION IS : how can I get the list of those BIG clients for any given month? Let's say I click on the November number of big clients in my table, could another table nearby display the list of those clients ?
Thanks in advance for your kind help, I've been trying for a while :)
I assume that you have a table of clients with the Name column with a one to many relationship with the Sales table and that you do not have duplicate client names. Then you may create a [BIG Sales] measure to be used in a table or matrix visual with client names on the rows.
since [BIG Sales] evaluates to BLANK() for clients with less that threshold sales, they are automatically filtered out from the visual
BIG Sales =
VAR threshold = 10000
VAR BigCustomers =
FILTER(
ADDCOLUMNS(
VALUES( Clients[Name] ),
"Sales", SUM( Sales[Amount] )
),
[Sales] >= threshold
)
RETURN
SUMX(
BigCustomers,
[Sales]
)
You could create a table or matrix visual with the client on the rows and use your measure in the values field. This will show 1 for all big clients and return blank for the rest (which should hide them). If you don't want to show the measure, you can set the value is 1 in the filter pane and remove the measure from the values field.
A more direct option is to use a simple SalesAmount = SUM ( Sales[Amount] ) measure in the values field and filter like this
I'm hoping someone can help as I've completely run out of ideas.
I'm working on performance reporting data, producing a number of visuals to summarise the most recent data. To allow users to retrospectively produce reports from previous quarters, I have added a date slicer as a way to "View data as at xxxx date".
Here's a rough representation of my data table - the due dates are in English format (dd/mm/yyyy):
The ratings are calculated in another system (based on a set of targets), so there are no calculated columns here. In reality, there are a lot more measures that report on different time periods (some weekly, some annually, etc) and there are different lags before the data is "due".
I eventually managed to get a measure that returned the latest actual:
MostRecentActual =
VAR SlicerDate = MAX ( Dates[Day] )
RETURN
CALCULATE (
SUM ( Data[Actual] ),
Data[Data due] <= SlicerDate,
LASTDATE ( Data[Data due] )
)
I'm not completely sure I've done it right but it seems to work. I'd be happier if I understood it properly, so explanations or alternatives would be welcomed.
What I'm trying to do now is a basic summary pie chart at the beginning which shows the proportion of the measures that were red, amber, green or unrated as at the date selected. So I would need it to count the number of each rating, but only one for each measure and only for the date that is closest to (but before) the slicer date, which would vary depending on the measure. So using the above three measures, if the slicer was set to 10/10/2019 (English format - dd/mm/yyyy), it would count the RAGs for Q3 2019/20 for measures A an C and for Q2 2019/20 for measure B as there is a time lag which means the data isn't ready until the end of the month. Results:- A: Amber, B: Green, C:Red.
If I were able to create the measure that counted these RAGs, I would then want to add it to a pie chart, with a legend that is "Rating", so it would split the chart up appropriately. I currently can't seem to be able to do that without it counting all dates before the slicer (not just the most recent) or somehow missing ratings from the total for reasons I don't understand.
Any help would be very gratefully received.
Many thanks
Ben
Further update. I've been working on this for a while!
I have created a COUNTAX measure to try to do what I was wanting to do. In some circumstances, it works, but not all and not in the crucial ones. My measure is:
TestCountaxpt2 =
VAR SlicerDate = MAX ( Dates[Date] )
VAR MinDiff =
MINX (
FILTER (
ALL ( Data ),
Data[Ref] IN VALUES ( Data[Ref] ) &&
Data[Data due] <= SlicerDate
),
ABS ( SlicerDate - Data[Data due] )
)
VAR thisdate =
MINX (
FILTER (
ALL ( Data ),
Data[Ref] IN VALUES ( Data[Ref] ) &&
ABS ( SlicerDate - Data[Data due] ) = MinDiff
),
Data[Data due]
)
RETURN
COUNTAX (
FILTER ( Data, Data[Data due] = thisdate && Data[Ref] IN VALUES ( Data[Ref] ) ),
Data[RAG]
)
It produces the following table for a subset of the performance measures, which looks almost ok:
Table showing the result of the TestCountaxpt2 measure:
The third column is the measure above and it seems to be counting one RAG per measure and the dates look correct as the slicer is set to 3rd January 2020. The total for column 3 confuses me. I don't know what that is counting and I don't understand why it doesn't add up to 7.
If I add in the RAG column from the data table, it goes a bit more wrong:
Same table but with RAG Rating added:
The pie chart that is produced is also wrong. It should show 2 Green, 2 Red, 2 Grey (no rating) and 1 Amber. This is what happens.......
Pie chart for the DAX measure, with RAG Rating in the legend:
I can see what it is doing, which is to work out the most recent due date to the slicer in the whole table and using that (which is 1st Jan 2020) whereas I want it to calculate this separately for each measure.
Link to PBIX:
https://drive.google.com/file/d/1RTokOjAUADGHNXvZcnCCSS3Dskgc_4Cc/view?usp=sharing
Reworking the formula to count the ratings:
RAGCount =
VAR SlicerDate =
MAX ( Dates[Day] )
RETURN
COUNTAX (
ADDCOLUMNS (
SUMMARIZE (
FILTER ( Data, Data[Data due] <= SlicerDate ),
Data[Ref],
"LastDateDue", LASTDATE ( Data[Data due] )
),
"CountRAG", CALCULATE (
COUNTA ( Data[RAG] ),
Data[Data due] = EARLIER ( [LastDateDue] )
)
),
[CountRAG]
)
Here's the table it produces:
The reason for Total = 4 for the third column is straightforward. The SelectDate is maximal over all of the Refs in the table and there are only four Refs that match that date.
To fix this and get the totals you're after, you'll need to iterate over each Ref and calculate the SlicerDate for each independently and only then do your lookups or sums.
I haven't tested this code but it should give you an idea of a direction to try:
MostRecentActual =
VAR SlicerDate = MAX ( Dates[Day] )
RETURN
SUMX (
ADDCOLUMNS (
SUMMARIZE (
FILTER ( Data, Data[Data due] <= SlicerDate ),
Data[Ref],
"LastDateDue", LASTDATE ( Data[Data due] )
),
"SumActual", CALCULATE (
SUM ( Data[Actual] ),
Data[Data due] = EARLIER ( [LastDateDue] )
)
),
[SumActual]
)
Going inside to outside,
FILTER the table to ignore any dates beyond the SlicerDate.
Calculate the LastDateDue for each Ref using SUMMARIZE.
Sum the Actual column for each Ref value using its specific LastDateDue.
Iterate over this summary table to add up SumActual across all Refs in the current scope.
Note that for 4, only the Total row in your visual will contain multiple Refs since the innermost Data table inside FILTER is not the entire Data table but only the piece visible in the local filter context.
New to PowerBI, so forgive me for the description here. I'm working with a dataset of retail headcount sensors, which gives me a table of locations, timestamps, and a count of shoppers:
Room TimeStamp Count_In
123 3/13/2019 8
456 4/4/2019 9
123 3/28/2019 11
123 3/18/2019 11
456 3/22/2019 3
etc...
I'm trying to calculate a running total for each "room" over time. The overall running total column is easy:
C_In =
CALCULATE (
SUM ( Sheet1[In] ),
ALL ( Sheet1 ),
Sheet1[Time] <= EARLIER ( Sheet1[Time] )
)
But I'm unable to figure out how to add that second filter, making sure that I'm only summing for each distinct location. Help is appreciated!
Your ALL function removes all context on Sheet1, try using ALLEXCEPT to keep the row context of the Room.
C_In =
CALCULATE (
SUM ( Sheet1[In] ),
ALLEXCEPT ( Sheet1, Sheet1[Room] ),
Sheet1[Time] <= EARLIER ( Sheet1[Time] )
)
I'm currently trying to calculate in a measure the last amount chosen in a checkout process. This needs to be identified by customer ID. The last chosen amount can be identified by using the Action ID (indexes order of events, but does not reset by customer).
Three columns are Action ID, Amount Chosen, and Customer ID (see image URL below).
EDIT 1/29/19: The output should also exclude $0's as an output option unless the Customer ID lacks a "Confirmed". The final purpose would be to have a sum of the "Desired Output".
Example data
Action ID Amount Chosen Customer ID Desired Output
1 $10 1
2 $15 1 $0
3 $20 2
4 $25 2 $25
5 $0 2
6 Confirmed 2
I would use a combination of the LOOKUPVALUE and MAXX functions, e.g.
Last Amount Chosen =
LOOKUPVALUE (
Table1[Amount Chosen],
Table1[Action ID], MAXX (
KEEPFILTERS ( VALUES ( 'Table1'[Customer ID] ) ),
CALCULATE ( MAX ( 'Table1'[Action ID] ) )
)
)
LOOKUPVALUE will pluck the value from a single row. MAXX is finding the highest Action ID for each Customer ID.
I have tweaked Mike Honey's answer to address the new requirements. I think this works - have only done mild vetting.
Last Amount Chosen =
VALUE(
LOOKUPVALUE (
TestData[AmountChosen],
TestData[ActionID], MAXX (
KEEPFILTERS ( VALUES ( 'TestData'[CustomerId] ) )
, CALCULATE ( MAX ( 'TestData'[ActionID] )
, FILTER(TestData, TestData[AmountChosen] <> "Confirmed")
, FILTER(TestData, TestData[AmountChosen] <> "0")
)
)
)
) * MAXX(TestData,if(TestData[AmountChosen] = "Confirmed",1,0))
This is basically the same work with a few extra filters to keep the special cases ('Confirmed' and '0') out of the result. Then we check for 'confirmed' status and get a flag value of 0 or 1. Multiplying that flag against whatever is returned by the lookup will either zero out the result if not confirmed or pass it through unchanged if it is.
It should be noted that this measure only provides meaningful information when the information is specifically grouped at the customerID level.