Power BI DAX Sum By Year - powerbi

Very new to Power BI and DAX and would appreciate a push in the right direction with this, seemingly simple, scenario, please. I have the following dataset:
Job Name Quarter Year Cost
alpha 1 2019 210
alpha 2 2019 100
alpha 3 2019 90
alpha 4 2019 28
beta 1 2020 100
kappa 1 2019 100
kappa 2 2019 90
beta 2 2020 100
beta 3 2020 75
beta 4 2020 30
kappa 3 2019 10
kappa 4 2019 30
All I am trying to do is get a measure/calculated column which calculates the total [Cost] per [Job Name] for each year. So for example I would get, for alpha, the value: 428. For kappa it would be: 230.
Thanks!

Use the below DAX to obtain the above result:
New Table :
SUMMARIZE('Table','Table'[Job Name],"New Column",SUM('Table'[Cost]))

Using SUMMARIZE (as #Shilpa suggests) will return/add a new table to your report data source. I think this is not your requirement.
As you are looking for a solution using a measure or a calculated column, lets know a bit details about them as they are not same with the functionality-
Calculated columns:
Evaluated for each row in your table, immediately after you hit 'Enter' to complete the formula
Calculate new values from existing values for each specific rows. For example, if you have value 5 in column "A" and 4 in column "B", you can create a new Calculated column C as (A x B) which will store the result 20. This will generate results in all rows using the calculation (A x B).
Result in Calculated column saved to the model as like other column's value.
Measures:
Evaluated when you use it in a visual and the visual is rendered
Measure always holds the aggregated value like - SUM, AVERAGE, COUNT.
Not saved anywhere (well, actually there's a cache in the report layer but it's not part of the file when you hit Save)
Now, for your scenario/requirement I think you need a simple measure as below-
total_cost = SUM('your_table'[Cost])
Your measure is ready now. Just pull column "Job Name" and measure "total_cost" to you visual. You will get your expected output. You can use slicer to check your value in/for different dimension. Just play around :)

Related

DAX AVERAGE() function differs from table average of column

I am new to DAX and running into a problem with regard to averages.
In PowerBI I am using a table with some dimensions and measures, the measures obviously require some form of summary so the dimension can roll up/summarize the data.
The problem is, I have a measure let's call Minutes that I want to average across the 5 lines of data, I drag the column minutes on set the summary in the table to average and it works perfectly by splitting the data according to the dimensions - data example:
1. A 10
2. A 8
3. A 10
4. A 7
5. A 5
6. B 10
7. B 10
8. B 9
9. B 9
10. B 10
Output in Table:
1. A 8
2. B 9.6
If I want to use that Average of 8 and 9.6 in another calculation and I create a new column called AvgMins = AVERAGE(Minutes) and drag it onto the grid I get a value of 8.8 for both A and B - I understand that the most likely reason for this is due to the calculation happening before the dimension splits and therefore the grid can't handle it - but how do I handle this in the DAX column calc itself?
As pointed out by Jos I was creating the calculation as a column instead of a measure, changing to a measure the normal AVERAGE() works perfectly
You should be using a Measure, not a Calculated Column.
A measure can result in a lot of different numbers - depending on the filter context.
AbgMins = AVERAGE('Table'[Minutes])
w/o any filter will return the average of the Minutes column, which is 8.8. But if you filter it by your category - A and B - it will return the average for all A's and all B's, which is 8 and 9.6.
If you are looking for Average of Average.
AVERAGEX(
VALUES('Table'[CategoryColumnName])
,CALCULATE(AVEARGE('Table'[Minutes]))
)
This if you are looking for Average per category
CALCULATE(
AVERAGE(
AVEARGE('Table'[Minutes]))
,ALLEXCEPT('Table','Table'[CategoryColumnName])
)
)

Calculating Year-over-Year Change using ISO Year and ISO Week in power BI

My backend table has the data at a week level. It contain the current ISO year and current ISO week, as well as, the previous year's ISO year and week number that the current year's data should be compared with.
For each signup_iso_year-signup_iso_week combination, there exists only one iso_prev_year-iso_prev_yearweek combination.
The iso_prev_year, iso_prev_yearweek columns account for the offset that might occur due to certain years having 53 weeks instead of 52.
data table
(I can't embed images, so I have added a table here as well, although it has much less information than the image in 'data table').
Number_of_signups
signup_iso_year
signup_iso_week
iso_prev_year
iso_prev_yearweek
Country
grade_level
5
2020
18
2019
18
IN
middle school
7
2020
18
2019
18
US
high school
6
2021
17
2018
18
IN
middle school
8
2021
17
2018
18
US
high school
I want to calculate to Year-Over_Year Change in number_of_signups using the signup_iso_year, signup_iso_week, iso_prev_year, iso_prev_yearweek columns.
I have already tried to create a calculated column that contains the sum of number_of_signups from previous year, but since every combination of country, grade_level, subject, email_type might not exist in previous or current year, some of the values are getting lost and hence giving incorrect results.
The answer I am looking for, is a Power BI measure that can give me the YOY change based on signup_iso_year and signup_iso_week.
Edit: I should have mentioned this before, but I forgot. The table contains data from 2018 to current day. So, the data size is quite large. Also, I need this YoY measure for a time series visual, which means that I can't assign ISOyear/ISOweek values for previous year using simple MAX/MIN functions. It needs to pick values from the iso_prev_year, iso_prev_yearweek columns but since EARLIER function can't be used in a measure, I am not able to figure out how to do that.
Which is why I had tried to create a calculated column, and use the EARLIER function to compute previous year's number_of_signups. But because of the other columns present in the data, i.e., country, grade_level, subject, email_type, there were discrepancies occurring in the actual number_of_signups and the calculated previous_year_number_of_signups. These discrepancies were due to the fact that not every combination of these columns exists for each week, so we might miss out on some data when calculating previous_year_number_of_signups.
Edit 2: Was asked to include examples of what the expected result would look like, so adding some pictures.
YoY at overall level, country level, grade level
YOY at country+grade level
If I understand your requirement correct, you need a Measure like below. Remember, this may not the exact one you need, but this will definitely help you to reach your required output.
prev_signup =
var iso_prev_year = MIN(your_table_name[iso_prev_year])
var iso_prev_year_week = MIN(your_table_name[iso_prev_yearweek])
RETURN
CALCULATE(
SUM(your_table_name[Number_of_signups]),
FILTER(
ALL(your_table_name),
your_table_name[signup_iso_year] = iso_prev_year
&& your_table_name[signup_iso_week] = iso_prev_year_week
)
) + 0
You can also do some transformation in Power Query Editor and join the same table using your Key columns. That case, you can bring previous year's value in the same row. Rest is just compare 2 columns from your table to calculate YOY

Power BI - Subtract values from same row

I have the following output from two tables.
Item# Desc Sales Rank1 Company Sales Rank2 Rank Diff
1 Wigit1 500 1 ABC 15 32 31
2 Wigit2 300 2 XYZ 80 16 14
Rank1 data is from table A and Rank2 data is from table B. Both tables are tied together with the Item#. I'm trying to correctly get the Rank Diff to calculate from Rank2 - Rank1.
Do this in Power Query with a simple merge and add a Custom Column with the arithmetic. This can all be achieved using the menu and options, no code required. This is the same as in Excel, so here's a walkthrough from Microsoft that goes through all the steps you might need. Once you start using Power Query you won't look back. Good Luck!
if you don't have excess to 'Transform Data'( Power Query new name), then simply go to modelling tab and create a new column
Rank_Diff= TableName[Rank2]-TableName[Rank1]

Showing last 4 values based on filter Power Bi

I have some Retirement fund data which I visualize with scatterplots and bar charts.
I am filtering the data with a Year slicer and a quarter filter (and fund type, but this is not relevant).
In my report I have 6 visuals, 4 of which are quarterly data, and two are yearly data.
I am using a bar chart visual to show certain values like the following:
My visual shows Quarter data for the selected Year. ( for example: it shows the invested equity per quarter for the year 2018)
However, for 2019 only 3 quarters are yet recorded.
Is it possible (for 2019) to show 3 quarters of 2019 and the last quarter of 2018 so that my bar chart still shows 4 quarters?
If so, could i make it so that the year is also visualized on the x axis so that one may not get confused as to what year the quarter is from? (i.e. show Q4 2018 and Q1, Q2, Q3 2019)
The best option is to have a current month indicator in your calendar table, which then counts back and forward in time. This example is based on Power Query and has current month and years/quarters/months ago. So for example March would be 0, Feb would be -1 etc.
You could also use something like in DAX as a calculated column:
Months = DATEDIFF([start],[end],MONTH)
with the Start being NOW() and the end the date in your calendar table
When the report refreshes it will recalculate the months ago and filter the visual so it will always correctly show the current Nth months you select. You could also use TOP, but if the calendar has dates in the future it might return those.
The top visual displays the unfiltered visual, the bottom with the 'Months ago' of 0, -1, -2, -3

Power BI: Calculating STDEVX.P over 6-Month period

I am attempting to calculate the most recent 6-Month STDEVX.P (not including the current month; so in May 2017, I'd like to the STDEVX.P for periods Nov 2016 - Apr 2017) for sales by product in order to further calculate variation in sales orders.
The Sales Data is made up of daily transactions so it contains transaction date: iContractsChargebacks[TransactionDate] and units sold: iContractsChargebacks[ChargebackUnits], but if there are no sales in a given period, then there will be no data for that month.
So, for example, on July 1st, sales for the past 6 months were the following:
Jan 100
Feb 125
Apr 140
May 125
Jun 130
March is missing because there were no sales. So, when I calculate STDEVX.P on the data set, it is calculating it over 5 periods, when in fact there were 6, just one happens to be zero.
At the end of the day, I need to calculate STDEVX.P for the current six month period. If when pulling the monthly sales numbers, it only comes back with 3 periods(months), then it needs to assume the other 3 periods with a zero value.
I thought about manually calculating standard deviation instead of using the DAX STDEVX.P formula and found these 2 links as a reference on how to do so, the first being closest to my need:
https://community.powerbi.com/t5/Desktop/Problem-with-STDEV/td-p/19731
Calculating the standard deviation from columns of values and frequencies in Power BI...
I attempted to make a go of it, but still am not getting the correct calculation. My code is:
STDEVX2 =
var Averageprice=[6M Sales]
var months=6
return
SQRT(
DIVIDE(SUMX(
FILTER(ALL(DimDate),
DimDate[Month ID]<=(MAX(DimDate[Month ID])-1) &&
DimDate[Month ID]>=(MAX(DimDate[Month ID])-6)
),
(iContractsChargebacks[SumOfOrderQuantity]-Averageprice)^2),
months
)
)
*note: Instead of using date parameters in the code, I created a calculated column in the date table that gives each Month a unique ID, makes it easier for me.
Your question would definitely be easier to answer with more explanation regarding your model. E.g. how you defined [SumOfOrderQuantity] and [6M Sales], since a mistake there could definitely impact the final result. Also, knowing what the result you're seeing is vs. the result you expect would be helpful (using sample data).
My guess, however, is that your DimDate table is a standard date table (with one row per date), but you want standard deviation by month.
The FILTER statement in your formula limits the date range to the prior 6 full months correctly, but it will still have one row per date. You can confirm this in Power BI by going into the Data View, selecting 'New Table' under Modeling on the ribbon, and putting your FILTER statement in:
Table = FILTER(ALL(DimDate),
DimDate[MonthID]<=(MAX(DimDate[MonthID])-1) &&
DimDate[MonthID]>=(MAX(DimDate[MonthID])-6))
Assuming you have more than one day of sales for a given month, calculating the variance by day rather than by month is going to mess things up.
What I'd suggest trying:
Table = FILTER(SUMMARIZE(ALL(DimDate),[MonthID]),
DimDate[MonthID]<=(MAX(DimDate[MonthID])-1) &&
DimDate[MonthID]>=(MAX(DimDate[MonthID])-6))
The additional SUMMARIZE statement means that you only get one row for each MonthID, rather than 1 row for each date. If your [6M Sales] is the monthly average across all 6 months, and [SumOfOrderQuantity] is the monthly sum for each month, then you should be set to go calculating the variance, squaring, dividing by 6, and square rooting.
If you need to do further troubleshooting, remember you can put a table on your canvas with MonthID, SumOfOrderQuantity and [6M Sales] and compare the numbers you expect at each stage of the calculation with the numbers you're seeing.
Hope this helps.
I was facing a similar problem while trying to calculate the coefficient of variation (Std. /Mean) by SKUS from sales data. I could use the Pivot-Unpivot function in Power Query editor to to do away with the problem of months with missing sales:
1) Export the data with any calculated columns
2) Reimport the data so that the calculated columns are also available in the power query editor
3) Pivoted the data by months
4) Replaced null values with 0s
5) Unpivoted the data
6) Close and apply the query
7) Add a calculated column for the coefficient of variation using the formula 
CV = CALCULATE(STDEV.P(Table1[Value]),ALLEXCEPT(Table1,Table1[Product]))/CALCULATE(AVERAGE(Table1[Value]),ALLEXCEPT(Table1,Table1[Product]))
Thus zero sales for the missing months will also be considered both for Standard Deviation and Mean.