Line chart Power BI - different value for different period of time - powerbi

I am using power BI and I would like to create a line chart which contains values from two tables (sales history and sales prediction). So for the past 12 months, the line should reprensent the sales history and for the next 6 months, the line reprensents sales prediction. Here is what the data looks like, lets say we are in June 2021:
I know there is a way to do it in DAX but I don't know how to do it by myself. Thanks a lot in advance for your help !

You can achieve this by
Delete any relationship between Calendar and the other two tables if there is any, as we are going to use DATESBETWEEN function to calculate
Create two metric like below, you might need to adjust the column names as per your project
Sales History Amount = IF('CALENDAR'[Date] <= TODAY(), CALCULATE(SUM('Sales History'[Amount]), DATESBETWEEN('Sales History'[Date], MIN('CALENDAR'[Date] ), MAX('CALENDAR'[Date]))), BLANK())
Sales Prediction Amount = IF('CALENDAR'[Date] >= TODAY(), CALCULATE(SUM('Sales Prediction'[Amount]), DATESBETWEEN('Sales Prediction'[Date], MIN('CALENDAR'[Date] ), MAX('CALENDAR'[Date]))), BLANK())
Add these two metrics in the table and use the Date from the Calendar table as X axis.
Format the first metric to solid line, and dash line for the second

Calendar should be linked to your tables.
Prediction Amount =
VAR lastSalesDate = MAX('Sales History'[Date])
VAR currentPredictionAmnt =
CALCULATE(
SUM('Sales Prediction'[Amount])
,KEEPFILTERS('CALENDAR'[Date]>lastSalesDate)
)
RETURN currentPredictionAmnt + SUM()
Sales Amount =
SUM('Sales History'[Amount])

Related

Power BI - Show zero/null value on line chart with dates

I'm using a simple table like this to make a report in Power BI:
Order number
Order date
Turnover
001
30/1/2022
10 €
002
30/1/2022
20 €
003
2/2/2022
15 €
I need to create a line chart showing all the dates, even where I have no data (no orders for that day). This is currently how is shown:
You can notice that the 1/2/22 and 3/2/22 are missing due to no order, but I want them to be visible and the value should be 0. This is also affecting the average line because it's calculated based on the days with data, but I need to put into account also the 0-turnover days.
I tried to use the "Show items with no data" on the date dimension and switch the X axis from Continuous to Catergorical and the other way around. I also tried to create a new metric like this:
Total Turnover = IF(ISBLANK(SUM(Orders[Turnover (EUR)])), 1, SUM(Orders[Turnover (EUR)]))
but it's not working.
If I understand your business requirement correctly, you are going to need to do three things:
Make sure you have a date-dimension table in your model. Build the relationship based on your [Order date] column.
Refactor your [Total Turnover] measure as such:
Total Turnover =
VAR TotalTurnover = SUM( Orders[Turnover] )
RETURN
IF(
ISBLANK( TotalTurnover ),
0,
TotalTurnover
)
Build your line chart using the [Date] column from your date table.

Calculate Average year to date in Power BI

I have a challenge in power BI to calculate average YTD.
enter image description here
I am looking for make the average for month two take in consideration data from month one and so on.
Any idea how to do that in Power BI?
Thank you in advance,
Mahmoud
Not knowing why the solution by Alexis didn't work for you, I've made the assumption that you only have one table (called 'Data'), with two columns as your image showed.
Step one:
Add two new columns (if you do this in Power query or power bi doesn't matter): [Month_name] and [Month_number].
[Month_Name] = FORMAT([Date]; "MMM")
[Month_number] = MONTH([Date])
Step two:
Add a measure
YTD Avg =
var currentMonth = MAX([Month_number])
RETURN
DIVIDE(
CALCULATE
SUM(Data[Amount]);
ALL(Data[Month_Name]);
Data[Month_number] <= currentMonth
);
CALCULATE(
COUNTROWS(DATA);
ALL([Month_Name]);
Data[Month_number] <= currentMonth
);
0
)
This should generate the following table:
Hope this helps. Please don't hesitate to ask if you have further questions.

power bi calculate sameperiodlastyear problem

I have a table of data for two years where I want to retrieve the previous years value for a date. my table the same date shown many times delineated by another value e.g.
Employee, Date, Sales, Prev yr sales
Sam, 1/07/2017, 100
Sam, 2/07/2017, 120
John, 1/07/2017, 90
John, 2/07/2017, 23
etc
Sam, 1/07/2018, 200, 100
Sam, 2/07/2018, 21, 120
John, 1/07/2018, 45, 90
John, 2/07/2018, 130, 23
etc
I am using a dates table created in DAX and have made a relationship between the dates in my table and the dates table.
I have created a measure for the sales & a column for the previous year sales. the latter using calculate and the sameperiodlastyear:
Prev Yr Sales = CALCULATE([Sum Sales],SAMEPERIODLASTYEAR('Calendar'[Date]))
my problem is I cant get the prev yrs sales value out in a table if there are multiple rows per date i.e. sam and john. if there is only one employee the function works.
Can anyone help?
When you are writing a calculated column with a CALCULATE in it, the row context from the table becomes the filter context you are working evaluating in. For this calculation, you don't want any of that row context except for the employee name, so you can remove it using the ALLEXCEPT() function:
Prev Yr Sales =
CALCULATE(
[Sum Sales],
ALLEXCEPT(Sales, Sales[Employee]),
SAMEPERIODLASTYEAR('Calendar'[Date])
)
I've done a quick simulation of the case you've described and found out that the reason you're not getting the expected results might be missing .[Date] on the end of your SAMEPERIODLASTYEAR function argument.
Check out the Data view on my simulation and the highlighted adjustment in the Prev Yr Sales measure:
And the matching result on a Matrix visualization on the Report view:
Hope it helps.

Power BI: Percent Change Formula

I want to create a simple percent change formula in power BI. Specifically, I want to evaluate month over month %change in cost. I know that percent change can be defined in different ways, so to be clear this is the formula that I am referring to:
%change = ([current value] - [previous value])/[previous value].
Here is an example in Excel of what I want to achieve:
I cannot find a way to target a specific row in a DAX formula. It seems that power BI only wants to include aggregate values such as sum, max, etc.
As a final note, I know that PowerBI includes a percent change "quick measure". However, the 'Base Value' always requires you to select aggregate measure instead of a single row value.
As Alexis mentioned, there is no concept of referring to a specific row in DAX. It can be a hard notion for people who are used to Excel, but if you want to take advantage of PowerBi, you will have to "unlearn" Excel way of thinking.
To accomplish your goal, you need to do some data modeling first. As a common practice, replace your "Date" column with a proper Calendar table:
Calendar table explained
As a result, you will have a data model in PowerBi that looks something like this:
Once you have such structure in place, DAX is simple:
Current Month Cost = SUM(Expenses[Cost])
Previous Month Cost = CALCULATE( [Current Month Cost], PREVIOUSMONTH(Calendar[Date]))
% Change = DIVIDE( [Current Month Cost] - [Previous Month Cost], [Previous Month Cost])
I used Earlier to get the previous date value for Open
Accum = var previousOpen=CALCULATE(MAX(EOG[Open]),FILTER(EOG,EOG[Date]<EARLIER('EOG'[Date],1))) return Divide(EOG[Open]-previousOpen,previousOpen)+1

Power BI Rolling Average

I've used the new Quick Measures feature of Power BI to build a 3 month rolling average calculation and it's working well. The equation is displayed below. However, when I try to use this metric in a time series visualization, the calculations are displaying three months past the current month, but I'd like for the calculation to stop at the current month.
I've played around with the __DATE_PERIOD variable to no avail. My date filter for the page is set to show all dates in the current months or 12 months prior via a calculated column on the date table.
Is anyone aware of how I can get the visualization to end at the current month?
Average Days to Close Rolling Average =
IF(
ISFILTERED('Date'[Date]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
VAR __LAST_DATE =
ENDOFMONTH('Date'[Date].[Date])
VAR __DATE_PERIOD =
DATESBETWEEN(
'Date'[Date].[Date],
STARTOFMONTH(DATEADD(__LAST_DATE, -3, MONTH)),
__LAST_DATE
)
RETURN
AVERAGEX(
CALCULATETABLE(
SUMMARIZE(
VALUES('Date'),
'Date'[Date].[Year],
'Date'[Date].[QuarterNo],
'Date'[Date].[Quarter],
'Date'[Date].[MonthNo],
'Date'[Date].[Month]
),
__DATE_PERIOD
),
CALCULATE(
'Closed Opportunities'[Average Days to Close],
ALL('Date'[Date].[Day])
)
)
)
In order to limit what is displayed within your chart, you need to filter the applicable date field so it only displays the dates you desire. In this case, you only want it to include dates <= today.
In order to automatically filter it when it is refreshed, I typically add a custom DAX column to the date table that I can filer on. In this case it would be something along the lines of:
excludeFutureDatesInd = 'Date'[Date] <= TODAY()
You can then add a visual, page, or report filter selecting all dates where [excludeFutureDatesInd] = True.
Not sure if you're still having issues with this, but I'd like to share a hack fix for those landing here. I fixed this issue by filtering on the base data (in your example, this would be "Average Days to Close"). Set a visual-level filter to include only those items where Average Days to Close > 0, and you should get the extra dates cut off the end of the graph.
So long as all of your base data passes through the filter, you should be good.