Calculate price based on distinct count - powerbi

I am having trouble working out a measure (Revenue) in power bi.
I have a measure which is basically counting distinct values in a table (table 1). From this column I want to multiply the distinct count to get the total price (prices are in another table).
See below for an example
Table 1
Product DistinctCount Revenue (Measure I am trying to Calculate)
A 15 45.00
B 30 60.00
Prices Table
Product Price
A 3.00
B 2.00
At the moment the Revenue is calculating based on COUNT and not DISTINCTCOUNT.
Any help would be much appreciated.
thanks!
Measures, Calculated Columns, Google

I am assuming you have a relationship set up between these two tables on [Product]. If this is the case you can do something like this to create a calculated column:
Revenue =
CALCULATE (
SUMX ( 'Table 1', 'Table 1'[DistinctCount] * RELATED ( 'Prices Table'[Price] ) )
)
If you are trying to create a table visual try the DAX below, where ID is just a transaction ID for each product in your 'Table 1':
Revenue =
VAR DistinctCountOfProductTransactions =
CALCULATE ( DISTINCTCOUNT ( 'Table'[Id] ) )
VAR Result =
CALCULATE (
DistinctCountOfProductTransactions * SUM ( Prices[Price] ),
TREATAS ( VALUES ( 'Table'[Product] ), Prices[Product] )
)
RETURN
Result

Related

power bi dax, sum up all latest monthly entries

Hi I have a data table in powerbi structured
id date data
1 2022-10-30 123
1 2022-11-01 130
1 2022-11-30 456
the data spans multiple user ids and multiple years and it the values are cumulative (like minutes on a phone plan for instance). This is not the real data
I want to add up the end of month data. In the ideal case, my table would be complete and 2022-10-31 would exist for instance, then I could do
Measure =
CALCULATE(
SUM( 'Table'[data] ),
'Table'[dates] = EOMONTH( 'Table'[dates],0 )
)
This returns 456 but I want 579 (123+456). So i cannot use EOMONTH
I think the answer is some combination of the dax above and
FILTER( Table, Table[date] = MAX( Table[date] ) )
Though if I paste that in solo, it grabs the actual latest date only, not all monthly latest dates
Also I will use slicers on user ID's in case that changes the DAX
Please use this measure to get what you need:
Measure_ =
VAR TblSummary =
ADDCOLUMNS ( YoursTable, "EOM", CALCULATE ( ENDOFMONTH ( YoursTable[date] ) ) )
RETURN
SUMX ( TblSummary, IF ( [EOM] = [date], [data] ) )
If we test our above measure on a table visual:

How to calculate average of `whole number` data type column by Category

I am currently facing an issue where I need to calculate Average of no. of approvers for each contract type. But the issue over here is that the Approver data i.e. (contract_approval_order_1) field is nothing but USER IDs ( whole number data type). Thus Power BI is not calculating the averages properly as shown in the image below.
I tried the following formula in the column:
Average Approver Order 1 Assigned =
DIVIDE (
COUNT ( view_contracts[contract_approval_order_1] ),
DISTINCTCOUNT ( view_contracts[contract_id] ),
0
)
I am expecting 0.5 as the average of the sample data given below.
Use the following dax formula to create a measure:
Average Approver Order 1 Assigned =
VAR __numerator =
CALCULATE(
COUNT ( view_contracts[contract_approval_order_1] ),
ALLEXCEPT( view_contracts, view_contracts[contract_approval_order_1] )
)
VAR __denominator =
CALCULATE(
DISTINCTCOUNT ( view_contracts[contract_id] ),
ALLEXCEPT( view_contracts, view_contracts[contract_type] )
)
Return
DIVIDE( __numerator, __denominator, 0 )
The ALLEXCEPT statement in the denominator variable may be slightly different if there are other filters ar slicers affecting your table.
This is the result

Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question. I am trying to get a count of the vehicles we have based on an EOMONTH date. We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.
I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):
SalesBlank = CALCULATE(
COUNT(Vehicles[MVANumber]),
FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))
I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.
How can I set up a measure to look at the date table and filter the vehicles table with this logic:
purchasedate <= date[EOMONTH] && ISBLANK(salesdate)
Any help would be greatly appreciated!!
Thanks,
Matt
Sample Data and Desired Results
Relationships
If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month. That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.
You can create an active relationship between Vehicle[PurchaseDate] and Date[Date]. Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].
You could use a measure that is something like this:
Inventory Count =
VAR MaxDate =
MAX ( 'Date'[Date] )
VAR MinDate =
CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR Sales =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR MaxPurDt =
CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
IF (
MIN ( 'Date'[Date] ) <= MaxPurDt
|| MIN ( 'Date'[Date] ) <= MaxSlDt,
Purch - Sales
)
This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them. The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table.
I'm not sure how to interpret your showing of just 3 months in the desired results. This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).
Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.

Calculate cumulative sum of summarized table column

I having trouble calculating the cumulative sum of a column on PowerBI.
I have a big offer table and I want to run a pareto analysis on it. Following many tutorials, I created a SUMMARIZED table by offer and a sum of their sales. So the table definition is:
summary = SUMMARIZE(big_table; big_table[offer]; "offer sales"; sum(big_table[sales]))
Many of the forums and stackoverflow answers I found have direct me to the following formula for cumulative sum on column:
cum_sales =
CALCULATE(
sum([offer_sales]);
FILTER(
ALLSELECTED(summary);
summary[offer_sales] <= max( summary[offer_sales])
)
)
However the resulting table is not correct:
What I need is simply to have the offers ordered by sales descending and then add the current row's sales amount to the previous row's sales,
So I excepted numbers closer to:
1st row: 1.5M
2nd row: 2.1M
3rd row: 2.6M and so on
But (maybe) because of my data structure and (certainly) lack of knowledge on how PowerBI works, I'm not getting the right results...
Total Amount = SUM ( 'Fact'[Amount] )
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", [Total Amount]
)
VAR CurrentOfferAmount = [Total Amount]
VAR OffersLessThanCurrent =
FILTER (
OfferSum,
[amt] <= CurrentOfferAmount
)
RETURN
SUMX (
OffersLessThanCurrent,
[amt]
)
There's no need to pre-aggregate to a summary table. We can handle that as in the measure above.
This assumes a single fact table named 'Fact', and a table of distinct offers, 'Offer'.
Depending on what you're doing in terms of other filters on 'Offer', you may need to instead do as below:
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", CALCULATE ( [Total Amount], ALLEXCEPT ( 'Offer', 'Offer'[Offer] ) )
)
...
The rest of the measure would be the same.
The measure is fairly self-documenting in its VARs. The first VAR, OfferSum is a table with columns ('Offer'[Offer], [amt]). This will include all offers displayed in the current visual. CurrentOfferAmount is the amount for the offer on the current row/axis label of the visual. OffersLessThanCurrent takes OfferSum and filters it. Finally, we iterate OffersLessThanCurrent and add up the amounts.
Here's a sample:

Cumulative average on power bi

I have a table which contains a list of products scores by date:
From this table, I have to make a plot of the cumulative percentage of each quality by date.
At this moment I have the percentage of each class by day:
For that I used this measurement:
Measure =
CALCULATE (
SUM ( Table1[Percentage_By_Class] ),
FILTER ( Table1, Table1[Date] = MAX ( Table1[Date] ) ),
ALLEXCEPT ( Table1, Table1[Score] )
)
/ CALCULATE (
SUM ( Table1[Percentage_By_Class] ),
FILTER ( ALL ( Table1 ), Table1[Date] = MAX ( Table1[Date] ) )
)
But this only considers the percentage of each day. I need to consider all previous dates. E.G. for day 2 I need to consider days 1 and 2, for day 3 I need to consider days 1,2,3 and so on.
How can I accomplish this?
in my opinion, you need a calendar for the date first, you can create a table easily bay dax function =CALENDARAUTO() And mark it as a calendar table,
After that, you can use a DATEMTD or a DATEYTD function for your coding purpose.
here are the steps:
1 - https://learn.microsoft.com/en-us/dax/calendarauto-function-dax
select left pane --> table --> modelling / create table and add dax formula
2- reference the table as a date table, right click on the table from the right pane
after then you can use data functions like YTD MTD ,
new measure :
1st mesure : AVG1 = AVG(DATA_)
2nd measure : YTD AVG ALL = CALCULATE([AVG1];DATESYTD(CALENDAR[DATE]))
REF: https://learn.microsoft.com/en-us/dax/dateadd-function-dax
then you can use MONTH(CALENDAR(DATE)) on left and YTD AVG as a value at any table...
regards.
SUNAY