Need a help in creating a dynamic "previous period" measure - DAX (Power BI) - powerbi

I need to create previous period measures for all of my sales measures and they should behave in a different way depending on the situation:
When selecting a non-continuous period no comparison should be made, show no data (not “blank” or “N/A”, just no data)
When selecting a continuous period containing the current period, compared to the parallel period TD as follows:
Today is 15/07 and the selected period is July/2023 – previous period should be 01-15/06/2023
Today is 15/07 and the selected period is June-July/2023 - previous period should be 01/04/2023-15/05/2023
Today is 15/07 and the selected period is 08/07/2023-15/07/2023 – previous period should be 08/06/2023-15/06/2023
When selecting a continuous period not containing the current period, compared to previous months as follows:
Selected period is June/2023 – previous period should be May/2023
Selected period is May-June/2023 – previous period should be March-April/2023
selected period is 08/06/2023-15/06/2023 – previous period should be 08/05/2023-15/05/2023
 
Any suggestion on how to write this DAX formula?
Any help will be much appreciated! 🙏
haven't try anything yet

Related

Replacing Missing Valuations with Adjusted Valuation - Finance/Accounting

I have a date set of with the relevant columns; Investment ID, Effective Date, Reported Valuation, Fundings, and Distributions.
Investments can report valuations on a either a Quarterly, Semi Annual, or Annual Basis - typically on the last day of the respective period. Therefore in a given year each investment can have a different number of valuation entries. You are always interested in having the latest valuation for any measured period or interval.
During a given period, there can be any number of fundings (where you add cash to an investment) or Distributions (where you receive cash from an investment). These can happen on any day during the year.
Transaction history table:
I would like this data to be able filter into a matrix that can drill into Monthly, Quarterly, and Yearly levels. The issue is that when I pull the most recent valuation for a given period using the below function:
Most Recent End Period Rep Valuation = CALCULATE (
    SUM ( 'TH'[ReportedValuation] ),
    LASTDATE ( 'TH'[Effective Date] ),
    'TH'[Valuation Bool] = 1 )
There are missing valuations for each respective investment and date (which occurs more frequently as you drill down the date hierarchy).
When you reach the month level, all investments have missing valuations in at least the first and second months of the quarter since none report that frequently.
Matrix with All Investments:
Matrix with only Investment A:
Matrix with only Investment B:
Matrix with only Investment C:
For the sake of tracking the valuation at any given time in the year, we use an Adjusted Value. An Adjusted Value is a way to estimate the financial value of the asset between its reporting dates. Its measured by taking:
(The last reported valuation)
+
(Fundings that have occurred between the date of the last reported valuation and the current point of measurement)
-
(Distributions that have occurred in the same period as the Fundings)
Using this calculation I can measure the intermediate valuation between periods to estimate my value up to the next reported valuation.
To illustrate: in the case of Investment C, it would be adding the running total of the fundings and distributions between 12/31/2020 and 12/31/21. that adjustment would have to happen for each investment for all the intervals its missing a valuation for.
So for Investment A, it would be measuring the adjusted valuation for the second and third month. For Investment B, it would be measuring for the next five months.
So I need to find a way to use or generate an adjusted value for the blanks in the matrix. I've considered the following solutions:
Use a measurement so that I can SWITCH() to use the most recent reported value() function OR use a new measurement that would calculate the correct adjusted value for all levels of the date hierarchy - the issue I've had with this is solving for each group by Investment ID before aggregating through the Most recent function.
Creating a new table that will have adjusted values calculated through a PowerQuery; my PowerBI skills are very limited so would love help on solving this! Mostly experienced in Excel and R.
PS: this a very simplified version of the data set I'm using. My other data set has more than 500 investments with 20 years of data (all active for different periods with no 'end date' value). So a measurement solution would be preferred so my data aggregates dynamic.

Power BI Dax for measuring an average over a changing horizon

We have a table in Power BI where we are trying to display the location, the current average of the location and then finally the average of the location based on the time period. Thus the location and the average stays constant, but the average across the location over the selected period changes with the period that one selects.
Do you have any advice on how we can implement this? Since the first two columns is linked to only the last ID/row (relationship) but the last column must go back to get the average across the selected period.

PowerBI dynamic filter for period

I would need a report that shows variances for the current period, next period, half-year and year-end.
There is a period filter from 1 to 12 representing the periods in a year.
I can build up a table that shows the difference:
for the current period (can be amended based on the period filter)
for full-year (using ALL function to ignore the period filter)
But I don't know how to write a dax that filter just for the first 6 periods as half-year.
Or add one to the current period filter to shows the next period-related variance.
Also, that should be dynamic. This means if I adjust the period filter it should show always be the next period. (If the filter is set for 1 to 4 it returns 1 to 5; set 2 to 7 returns 2 to 8).
Could you please help me with how to do that?
Thanks
So, let's say we have 2 databases.
The plan for the current year and the actuals:
Excel
And I would like to see the difference between them in one visualization.
If the period set is 1 to 2:
for the current period shows: sum plan (1-2) - sum Actual (1-2)
next period: sum plan (1-3) - sum Actual (1-3)
Half Year: sum plan (H1) - sum Actual (H1)
FY: sum plan (1-12) - sum Actual (1-12)
I am not able to understand exact query but it seems that as per your given examples it's more related to time intelligence formulas in PowerBi to given results based on specific time frames. (YTD, QTD etc)
Below is the link to address your query via time intelligence formula
https://www.analyticsvidhya.com/blog/2020/08/time-intelligence-power-bi/

How can I pass user input from a what-if parameter to be used in a calculation in a measure?

I'm new to Power BI so please bear with me, I've searched extensively for a solution but haven't been able to find quite the right thing.
I have a data set that contains (among other things) transactions with amounts and financial periods stored as whole numbers, and what I need to do is use those to arrive at YTD totals. So the user would select a period from 1-12 and then the YTD measure should calculate the totals for the selected period PLUS the previous periods (said another way, it should filter OUT the periods greater than the selected period).
I've tried to filter the totals for the appropriate periods by referencing the selected value of the parameter using "<=" but haven't been able to make that work:
ytd = CALCULATE(SUM(data_tbl[Calculated USD]),data_tbl[PERIOD NUMBER]<=current_period[current_period Value])
I'm probably approaching this the wrong way, but somehow I need to calculate YTD based on the user selecting the desired period. Thanks for any advice.

Calculate Event Durations to only part within Sliced Period

We are investigating PowerBI for a reporting solution and it does a lot of what we need. However we need to be able to do adhoc reporting on events. Each event has a start date and an end date and total seconds and do percentage calculations etc. This works really well.
However our common requirement is to specify a start date and end date of which we wish to consider data. Many of the events will span over start and end of required period or even start before the start of period and go beyond the end. What we need to do is only consider the part of events that falls within the period.
Is it possible to use a slicer to define a start/end period and then only include the number of seconds within the period sliced for each event?
This would allow us to calculate the total time for all events that fell within the period.
Update
My table consists of hundreds of thousands of rows like
EventID|VehicleID|StatusID|ReasonCodeID|StartDateTime|EndDateTime|TotalDuration
We need to look at portions of each event that fall within a selected period (start/end). However events can span periods. If part of an event is outwith a period we want to ignore that part of the duration. If whole event is outside selected period then we would ignore all of it.
For example say an event starts on 1st Feb and goes to 1st July. If the selected date range of slicer was 1st Jan to 1st March then I want to only include the time between 1st Feb and 1st Mar in Total Duration calculation
What you're asking for is doable, but how well it works would very much depend on the amount of data in your table.
You can use a DAX measure to calculate the sum of the duration for any events that fall on a given date. You can do this using a disconnected date table that you slice on (the October 2016 Power BI Desktop release includes a built-in data slicer that allows you to easily pick a range). More information on that here: https://powerbi.microsoft.com/en-us/blog/power-bi-desktop-october-feature-summary/#reportView.
You would then have a CALCULATE measure that SUMS the duration, with FILTER to ensure the event in question lands within the date range selected by the slicer. There's more specifics on this part of the question here: Optimizing Dax & model for "where date between" type queries
However, you're going one step further, in that you don't want to SUM the full duration of events that fall within a given date range - you only want to sum the duration that falls within the given date range.
In order to do that, you have to calculate the duration for each individual row at run time based on the selected date range. You can do this with a measure that uses SUMX (see below), but over a large number of records (thousands, millions) the calculation will start slowing down.
For example, if you have a disconnected date table called Date, and your event table is called Event, you can have a measure such as:
Filtered Duration =
CALCULATE (
SUMX (
Event,
DATEDIFF (
MAX ( MIN ( 'Date'[Date] ), Event[StartDateTime] ),
MIN ( MAX ( 'Date'[Date] ), Event[EndDateTime] ),
SECOND
)
),
FILTER (
'Event',
'Event'[StartDateTime] <= MAX ( 'Date'[Date] )
&& 'Event'[EndDateTime] >= MIN ( 'Date'[Date] )
)
)
MIN('Date'[Date]) in this case formula corresponds to the earliest date in the disconnected date table that is within the selected date range. MAX('Date'[Date]) corresponds to the latest date.
The last part (the FILTER) is saying "only look at events that land on a date within the range selected". The SUMX is saying "for each row, do a DATEDIFF". The MAX within the DATEDIFF is saying "choose the later of either the first date from the slicer, or the start date of the event". The reason for this is that if you select the 15th through the 20th on your date slicer, and an event starts on the 18th, you want to count from the 18th. But if the event started on the 11th, you'd want to count from the 15th. The MIN is doing the opposite with the end date.
If an event falls within the time range entirely, then it will calculate the seconds from start to end. If the end of the event is after the selected time range (for example), then it will count the seconds from the exact start of the event to midnight of the selected end date.
Note that because I've used a date table you wouldn't be able to pick partial days as a date range. You could extend this to include a time table but it gets more complicated (you'd need a separate start & end time table and then account for that logic in the above, already quite complex, formula)
Is it possible to use a slicer to define a start/end period and then
only include the number of seconds within the period sliced for each
event?
Yes, It is possible. In the Power BI Gallery there is a custom visualization that lets you filter date ranges.
You just have to drag and drop the date column in your model to the Timeline slicer in order to filter your measures.
To install it go to Gallery and search timeline in the search box, download it and import it to Power BI.
If you get stuck with the DAX expressions, include SQL code and a sample model to your question.
Let me know if this helps.