power bi calculate sameperiodlastyear problem - powerbi

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.

Related

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

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])

Measure (CALCULATE) - two fact tables

Hi DAX Experts around the world,
I hope you can help me with below problem. In short:
there are two fact tables: Table1 and Table2 , not connected between each other and cannot be appended.
they have both relation to Calendar table
I need to create a measure which will return some part from Table1 and a part form Table2, as described on the picture.
When a Nov-21 is selected on the slicer, measure should return Actuals for first 10 months of the year with 100 each month (from Table1) and remaining 2 months with Forecast values from with 200 each month (from Table2).
Is it possible at all as I am out of ideas ?
Thank you in advance.
Max
You can create a copy of the calendar table(Calender (2)), which can be used to create a slicer.
With the help of this table, you can create a measure to calculate the value based on selected date like this:
Amount Measure =
VAR _selected_date = MAX('Calender (2)'[Date])
return SUMX(SUMMARIZE(Calender,Calender[Date]),IF('Calender'[Date]<_selected_date,SUMX(FILTER(Table1,Table1[Version]="Actual"),[Amount]),SUM(Table2[Amount])))

Finding Previous year Sales in Power BI with week number

I am trying to find the Previous year sales for the same week in Power BI.I dont have any date column.
I have two table one is FACT Indicators table as shown below:
and one sales table( Fact Sales table):
I want to create one calculated field namely(Sales Previous Year) to show the previous year sales for the same week .
In 'Fact Indicators' table 'PY 52 week flag' filed shows if this week id is Previous year or not.
Week column shows the week number from 1 to 52 weeks .
Week Id shows the unique number per Market key.
'Market_Week Id Key' is the common joining key between FACT Indicators table and Fact Sales table
Please help me to find the formula for calculated field.I dont have the date field in my raw data
Every time you deal with anything related to dates, you will need to add what we call a date dimension. It will save you tons of headaches. Once you have it in you will be able to hook it into the creation of the calculated filed.
you can google power bi or ssas date dimension and find tons of information on it.
Yeah! I guess SQL Technical team can be a tough crowd.... Well! In this case, I would recommend bringing the Year into FactSales Table from Fact Indicator . You have two options here with physical relationship set up between Market Week Id Key in both tables you can build a calc column with
Year = CALCULATE(VALUES(FactIndicators[Year]))
or without relationship use LOOKUPVALUE on WeekId
Year = LOOKUPVALUE(FactIndicators[Year], FactIndicators[WeekId], FactSales[WeekId])
Sales Last Year calc colum :
SalesLastYear =
CALCULATE (
SUM(FactSales[SalesThisYear] ),
TOPN(1,
FILTER(
FactSales,
FactSales[Year] < EARLIER(FactSales[Year])
&& FactSales[Key] < EARLIER(FactSales[Key])
)
)
)

Calculate Works until i add FILTER

I'm learning DAX and I'm trying to make a measure for Sales Last year.
this formula works :
Sales LY = CALCULATE([Sales CY],SAMEPERIODLASTYEAR(Date[Date]))
But when I add FILTER to the measure, it gives me the values from the current year like "Sales CY"
Sales LY = `CALCULATE([Sales CY], Filter (dim_date, SAMEPERIODLASTYEAR (Date[Date]) ))`
I already have a date filter on the page relative date in this year
the invoices Table and Date table are joined on the date of the creation of the invoice (createdat)
Any ideas what's the meaning behind the things in blue circles?
The FILTER function expects a condition that can be checked for each row of the table instead of a column of dates returned by SAMEPERIODLASTYEAR, so I wouldn't expect the second version to work.
I think the bits circled in blue are emphasizing that you are connected an imported data table to a DirectQuery table (different storage modes).

Dax Finding date based on Criteria calculated Column

I couldn't find an answer for my issue elsewhere, so trying my luck here.
I have sales table and my final result should determine if there were sales made for same person in specific period of time, for example within 7 business days. for example:
For ID 123 I have to flag it that sale for products A,B,C where within specified period.
For ID 1234 only sales of products A and B meet the criteria product C was sold in irrelevant time frame.
I've created a date table with indicators that determine for each date if the date is a working day, but i am struggling to calculate the relevant last working day
For example: I need that for 01/01/2019 i will get 10/01/2019 date, based on NUMOFDAYS and FinalWorkday = TRUE, which basically means that i have to count NUMOFDAYS times TRUE statement for each date and return corresponding date.
After that step done I think that it would be pretty easy for me to determine if the sale for a person was made in specific time frame.
If someone can give me direction for that much appreciated
Thank you in advance
You could use a DateTable like this one:
I used the following DAX-expressions for the calculated columns:
nrDays = 7
isWorkDay = WEEKDAY('DateTable'[Date],2) < 6
rankWorkingDays = RANKX ( FILTER ( DateTable, DateTable[isWorkDay] = TRUE () ),
DateTable[Date] , , ASC )
LastWorkDay = LOOKUPVALUE ( DateTable[Date],
DateTable[isWorkDay], TRUE (),
DateTable[rankWorkingDays], DateTable[rankWorkingDays] + DateTable[nrDays])
This issue can be solved by the following, since three are non-working days/holidays we can filter them out via POWERQUERY, than add an Index Column and Another column Which is basically Index column + Number of days wanted, then simply merge duplicate of dates query on Index+number of days wanted column on Index column. And we get the correct date