Display last 12 months - powerbi

I am lookking to display the last 12 months and the data for selected month. If a user selects december 2019, it will dsipaly all months from december 2018 till december 2019.
Example
Here is the pbix file https://drive.google.com/file/d/1WBOolZIFNTWQsPt_QvCY5OAmdy80lHzm/view?usp=drivesdk

Make sure that your Label_Month table is not connected to your fact table. Also, click on "Modeling" in the desktop editor and "Manage Relationships"; make sure that the filter value for Label_Month is not limiting your output table. Make sure that there is an actual date value being selected in Label_Month table; if there is not one add it. You will do the date limiting in the measure value that you aggregate. This measure assumes that you are summing, but you can use other operators in Calculate.
Measure = VAR StartDate = DATEADD(MAX(Label_Month(ActualDate)),-1,year) VAR EndDate = MAX(Label_Month(ActualDate)) RETURN
CALCULATE( SUM(Column), FILTER( ALL(Label_Month), Label_Month(ActualDate) >= StartDate && Label_Month(ActualDate) <= EndDate ) )

Related

IF 'AND-OR' ISFILTERED combination in DAX giving problems

Below is the sample dataset
The data has two slicers ( date and category ) shown below
I am writing a DAX Statement to multiply the sum(values) * 10 only if the date range is in the current year 2023.
The StartYear gives the start of the current year, firstD gives the lowest date from the date slicer.
Formula =
var new = sum(Test[Value]) * 10
var startyear = DATE(YEAR(TODAY()),1,1)
var firstD = CALCULATE( MIN( Test[Date]), ALLSELECTED(Test[Date]) )
return if( ISFILTERED(Test[Categories]) && firstD >= startyear, new, 0 )
Now when I filter dates to 2023, the total value should be 2300 but it shows as 0
However the DAX works when I select A or B
If we remove the ISFILTERED function then, it gives wrong value, the expected value is 0 because the start date is in 2022, but it shows 650
let me know if that is the right syntax
It looks like you are not using a separate calendar table to handle this, which you need!
In your very last example you have set your slicer to some time late 2022, but the minimum value of 'Test'[Date] for your selected category is in year 2023. Hint: set the slicer to e.g. 2022-12-14, this will include a 2022-date for Category A in your data.
Your measure behaves exactly how it is supposed to, in other words!
To fix this, you need to do the following:
Create a calendar table in your model, this should contain contiguous dates, which is necessary for the filtering method you want
Establish a relationship between the calendar table and existing Test table.
Use the date column from your new calendar table in your slicer and as date reference in your measure
Exactly how to create a calendar table is thoroughly documented on Google, I recommend you search and find an article or video you understand for implementing this.
Lastly: Your use of ISFILTERED in this measure seems strange, since you mention nowhere the requirement of only showing a number if the column you are testing filtering on is filtered, if that makes sense.. :-) The way you describe your calculation, you only need to check whether the selected date range starts in current year.

How to show the records which are part between selected date range in Dax

I have a below table in Power BI. This table will have the details of campaigns which are ran on specific period.
Campaign Name StartDate Enddate
AAA 01-05-2022 30-04-2022
BBB 01-04-2022 30-04-2022
CCC 01-04-2022 30-04-2022
DDD 01-04-2022 30-09-2022
EEE 01-03-2022 30-09-2022
FFF 01-03-2022 30-09-2022
Now i am using the start date in the slicer. so if i select date range of Apr-22 to Jun-22, table should display whatever campaigns which are active between the selected period. In this case, it should display all the values from the above table, because all the campaigns are between Apr-Jun. but in my case, last two rows are not displaying since the start date is in March but these campaigns are also active during the period of Apr-22 to Jun-22. is there a way we can create some measure to show the campaigns, even if start date before selected slicer date range but end date falls between the selected date range?
You can try smth like this. The measure should return 1 for “active” if a company name is in scope and total number of active in total. But it would be better to create a DateTable as Ozan Sen advise. Otherwise you can get a wrong output - a slicer will return max and min dates that you have in table not as slicer shows. For instance your slicer shows you the first date as 01 Jan, but in your facts the minimum is 3 Jan, so, this you will get as minDate. The problem can come if the endDate is 02 Jan, the measure will not count it, because the minDate=03 Jan. With a Date table you'll avoid this kind of problem. I didn't check the measure, so I'm not 100% sure it works.
VAR minDate =Min('table'[startDate])
VAR maxDate =Max('table'[startDate])
VAR noOfCompNames =
CALCULATE(
CountRows(table)
,OR(
AND('table'[startDate]>=minDate,'table'[startDate]<= maxDate)
,AND('table'[endDate]<=minDate,'table'[endDate]>=maxDate
)
)
RETURN
noOfCompNames

Get year of end date within date between

I use as a filter date between.
I would like to get the year of the end date. I try as measure yearmax=year(max(date)) but it returns wrong values. I need to extract only the year of the end date. For example, when the user select as date range 01/03/2020 till 04/04/2027, I need to get only the year of the end date and have 2027.
I put the pbix file here, any help would be appreciated.
https://drive.google.com/file/d/1AqrdiHBQdYjrQImP9AGO_sC7Duh9Yeb5/view?usp=drivesdk
This looks like a Power BI slicer visual bug.
You set filter into the slicer to restrict the selection over the last 90 days including TODAY. When selecting any day up to yesterday, your YEAR(MAX('Date'[Date])) measure works fine. But when you push the slicer up to today, then even if the slicer shows the correct date of today in the upper bound, the measure returns the last year in the date table, like when no filter exists. To fix that you might add a filter to your measure to limit the upper bound to TODAY() like follows
Year end date =
VAR MaxDate =
CALCULATE ( MAX ( 'Date'[Date] ), KEEPFILTERS ( 'Date'[Date] <= TODAY () ) )
RETURN
YEAR ( MaxDate )
Of course, maybe that the problem was just the existence of the filter in the slicer, and by removing it the original measure works ad desired

Filter table based on a specific date plus 7 days

I have a table containing a date field (from 1 March 2020 to now) that I need to filter to a specific date and the previous 6 days to give complete week's data. So if I chose 30 March I'd get a table of 24 March to 30 March. If I then chose 31 March the table would show 25 March to 31 March.
I can use a date slicer to choose a range of dates but I want to be able to pick a single date, with Power BI automatically selecting the earlier date.
Any pointers much appreciated.
Mark.
You can create two measure - one for Slicer selected date and Another one with 7 day minus from the selected date as below-
Considering your date table name is- Dates
selected_date = SELECTEDVALUE(Dates[Date])
seven_day_starts_from = DATEADD(Dates[Date],-7,DAY)
Now create your calculated measure first like-
total_sales = SUM(Sales[sale])
Here comes how you will always calculate last 7 days sales considering the selected date in the slicer-
7_day_sales =
(
CALCULATE(
[total_sales],
DATESBETWEEN(
'Dates'[Date],
[seven_day_starts_from],
[selected_date]
)
) + 0
)
Remember, this is just a sample flow showing how it should work. You should try to follow the steps with your data and table structure. Dates table is a calendar table and Sales table is connected to the Dates table using the Date column.

Override Slicer Filter Context in Chart (To Show Fuller X-Axis)

Imagine I have a fact table with sales spanning 3 years (2016-2018). I have a chart showing sales by month (36 points on the X-Axis). I have a slicer selection to Year = 2018, and Month = June.
Is it possible, with a measure, to show on a chart, the trailing 6 months from the slicer selection? In other words, with the slicer still set to Year = 2018 and Month = January, can the chart display 6 points (the trailing 6 months)?
How would this be accomplished?
The approach I would use in this case would be to create a parameter table for the date which doesn't have a relationship with my other tables and use that date for the slicer. Then you'd write the sales measure you use on the chart to read the selected date and return blanks for any dates not within the range you want.
Roughly like this:
NewSalesMeasure =
VAR SelectedDate = SELECTEDVALUE(Slicer[Date])
VAR CurrentDate = SELECTEDVALUE(Sales[Date])
RETURN IF(CurrentDate <= SelectedDate &&
CurrentDate > DATEADD(SelectedDate, -6, MONTH)
SUM(Sales[Amount]),
BLANK()
)