power bi dax, sum up all latest monthly entries - powerbi

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:

Related

Running total with a slicer in Powerbi

I am trying to find the running total by month, it worked using the following measure but only without using a slicer for the category "BLK":
With Date = VAR MaxDate =
MAX ( 'Calendar Table'[Date] ) RETURN
CALCULATE (
SUM ( 'Injuries'[Total] ),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
),
FILTER ( ALLSELECTED ( 'Calendar Table' ), 'Calendar Table'[Date] <= MaxDate)
)+0
The result was only accurate when I don't apply the Category slicer "BLK", I tried a different measure:
With BLK = CALCULATE( SUM('Oxy Oman Personal Injuries'[Total]),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
), ALLSELECTED('Blocks'[BLK]) )+0
The above gave me something similar to the actual data, it did not sum up the numbers.. at least it gave me the correct total of a the category selected = 5 while the first measure gave the total wrong = 4.
Here is a screenshot of the results for both measures with and without a slicer:
Without BLK slicer:(With Data gave me what I wanted)
Using one Category in the BLK Slicer:
How can I fix that?

dax - counting rows between dates

I have a table like
Account Open Close
1 01/01/2018 01/01/2019
2 01/01/2018 01/01/2020
3 01/01/2019 01/01/2021
4 01/01/2021
5 01/01/2019 01/01/2020
I'm interested in counting the number of accounts that are still active at the end of each year:
Year Count
2018 2
2019 3
2020 1
2021 1
I'm not sure if this can be derived from the Account table itself, so I created a date table with dates spanning many years. I added a column like
active_accounts = countrows ( Accounts, FILTER ( Accounts[Open] >= Date_table[Date] && Date_table[Date] < Accounts[Close]
The formula seemed to be working as an added column but took extremely long to calculate as the date table contains many dates. So I tried to use the formula as a DAX measure, but it seems to have trouble comparing columns between more than one table:
a single value for column 'Date' in table 'date_table' can't be determined. This can happen when a measure refers to a column containing many values without specifying aggregation
What's the simplest way to accomplish counting the number of active accounts in a particular year? Can this be done without a date table?
edit: enclosing date_table[date] with min() and max() makes the measure valid, but the figures are not right.
-further research indicates this might require CROSSJOIN()
edit: it looks like this can be accomplished by creating a cartesian product b/w the date_table and Account by FILTERING on where Date_table.Date is greater than Open but less than Close
You have already created a Calendar table and you can achieve the end goal with the following measure
_mX:=
VAR _1 =
GENERATEALL (
'fact',
DATESBETWEEN ( 'Calendar'[Calendar_Date], 'fact'[Open], 'fact'[Close]-1 )
)
VAR _2 =
ADDCOLUMNS (
_1,
"Calendar_Year",
CALCULATE (
MAXX (
FILTER ( 'Calendar', 'Calendar'[Calendar_Date] = [Calendar_Date] ),
'Calendar'[Calendar_Year]
) + 0
)
)
VAR _3 =
SUMMARIZE ( _2, [Calendar_Year], [Account] )
VAR _4 =
GROUPBY ( _3, [Calendar_Year], "count", COUNTX ( CURRENTGROUP (), [Account] ) )
VAR _5 =
ADDCOLUMNS (
'Calendar',
"ct",
CALCULATE (
MAXX (
FILTER ( _4, [Calendar_Year] = MAX ( 'Calendar'[Calendar_Year] ) ),
[count]
)
)
)
RETURN
MAXX ( _5, [ct] )
The measure is depended upon a calendar table with minimum columns like following and calendar has no relationship to fact
| Calendar_Date | Calendar_Year |
|---------------|---------------|
| 1/1/2018 | 2018 |

DAX - Get list from a filtered SUMMARIZE formula

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

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 price based on distinct count

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