How to calculate % in sales when we calculate sales amount in measures? - powerbi

If sales and profit amount presents in row by row then it would easy to calculate % of sales.
% of Total Sales =
VAR Allsales =
CALCULATE(
Orders[ALL Total Sales],
ALL(Orders)
)
VAR Ratio =
DIVIDE([Total Sales],
Allsales)
RETURN
RATIO
and then I created another sales amount by using measures like this Sales Amount = CALCULATE(SUMX(
'Sales by Store',
'Sales by Store'[quantity_sold] * 'Sales by Store'[unit_price]))
and when I create % of sales I am unable to divide the sales / all sales * 100 pls help me out [enter image description here][1]
% of Total Sales =
VAR Allsales =
CALCULATE('Sales by Store'[Sales Amount])
VAR Ratio =
DIVIDE('Sales by Store'[Sales Amount],Allsales)
RETURN
RATIO

Related

How to create a measure that sums rows only where sales exists this and last year?

Hello community and DAX gurus!
I am trying to create a measure that calculates the total product sales for a specfic month only for does products that has been sold the same period last year.
Below is an illustration of what I want to achieve:
The first thing I did was to create a measure to calculate the Sales Amount for previous year:
Sales Amount PY =
CALCULATE(
[Sales Amount],
SAMEPERIODLASTYEAR(DimDate[Datekey])
)
The second thing I did was to create a Comparable range flag as measure:
ComparableRange = IF(FactSales[Sales Amount] = BLANK() || FactSales[Sales Amount PY] = BLANK(),0,1)
Third step I created a measure to calculate the total product sales:
Total Product Sales =
CALCULATE(
FactSales[Sales Amount],ALL(DimProduct)
)
The final step I want to calculate the total product sales only for does products being comparable.
I tried this solution but not getting it to work, it is only returning blank:
Total Product Sales Comparable =
var CompRangeTable = ADDCOLUMNS(FactSales,"#CompRange",[ComparableRange])
var FilteredTable = FILTER(CompRangeTable,[#CompRange] = 1)
return
CALCULATE(FactSales[Sales Amount],ALL(DimProduct),FilteredTable)
I also tried this solution but still getting blanks:
Total Product Sales Comparable =
var FilteredTable = FILTER(FactSales, [Sales Amount PY]*[Sales Amount]+0<>0)
return
CALCULATE([Sales Amount],ALL(DimProduct),FilteredTable)
I wonder if the issue is that the Comparable range flag doesn't evaluate during context in the measure and potentially only returning 0 and if that is the case how would you go about to solve this problem.
To demonstrate my problem I have used the ContosoRetailDW sample database with a simple star scheme consisting in the tables "FactSales", "DimDate" and "DimProduct"
This expression
ADDCOLUMNS(FactSales,"#CompRange",[ComparableRange])
is equal to
CALCULATETABLE(
ADDCOLUMNS(FactSales,"#CompRange",[ComparableRange])
,Calendar[CalendarMonth]=2000805
,DimProduct[Brand]="The Phone Company"
)
so :
1 - FactSales is cutted by context
2 - ADDCOLUMNS applies a row context to [ComparableRange] measure
to EACH row.
For example you have a row with FactSales[date]="01/01/2022"; FactSales[product]="iPhone"; FactSales[customer]="Bill Gates" ; FactSales[price]=200 ; FactSales[qty]=10 Your [Sales Amount PY] in [ComparableRange] will search SAMEPERIODLASTYEAR() on a day level, for the sample it is - "01/01/2021" most probably you have no sales for customer "Bill Gates" that date, so [ComparableRange] will return you - 0
Try this one, it's not optimized, so just check if it works.
Total Product Sales Comparable=
VAR CurrentCalendarMonth = SELECTEDVALUE(Calendar[CalendarMonth])
VAR allProducts =
CALCULATETABLE(
VALUES(DimProduct[ProductName])
,ALL() -- remove all filters and crossffilters from your data model
)
VAR totalSalesAndCompRng =
ADDCOLUMNS(
allProducts
,"#totalAmount
,CALCULATE(
[Sales Amount]
,Calendar[CalendarMonth] = CurrentCalendarMonth
)
,"#CompRange"
,CALCULATE(
[ComparableRange]
,Calendar[CalendarMonth] = CurrentCalendarMonth
)
)
VAR onlyCompRng =
FILTER(
totalSalesAndCompRng
,[#CompRange]=1
)
RETURN
SUMX(onlyCompRng,[#CompRange])
Your second measure:
Total Product Sales Comparable =
var FilteredTable =
FILTER(
FactSales
,[Sales Amount PY]*[Sales Amount]+0<>0 -- returns false
-- the reason is the same
-- as above
-- and FilteredTable is empty
)
RETURN
CALCULATE(
[Sales Amount]
,ALL(DimProduct)
,FilteredTable
)
You can try smth like this:
Total Product Sales Comparable =
var FilteredTable =
FILTER(
All(DimProduct)
,NOT [Sales Amount PY]*[Sales Amount]=0
)
VAR CurrentCalendarMonth = SELECTEDVALUE(Calendar[CalendarMonth])
RETURN
SUMX(
FilteredTable
,CALCULATE(
[Sales Amount]
,Calendar[CalendarMonth]=CurrentCalendarMonth
)
)

Calculate the total annual sum for the previous year in Power BI

How can I calculate the total annual sum for the last year? Specifically, I need to calculate last year's total sales.
Here's what I do:
total_sales_last_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
DATEADD(dim_date[formatted_date], -1, YEAR),
REMOVEFILTERS(dim_date[month_name])
)
However, if for example, I filter for January, in sales_last_year I won't get the sales of the whole year, but I will only have those of January 2021. The result I'm looking for is the total of the previous year.
Solution:
total_sales =
VAR last_year = YEAR(MAX(dim_date[formatted_date]))-1
VAR sales_last_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
FILTER(ALLEXCEPT(dim_date, dim_date[year]), YEAR(dim_date[formatted_date]) = last_year )
)
VAR sales_current_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
DATESYTD(dim_date[formatted_date])
)
RETURN sales_last_year + sales_current_year

DAX - Time Intelligence - Year to Quarter

My Dataset
am trying create a report based on the selected quarter
What I want is sum of sales by quarter grouped by product in one column and sum of sales for the year until selected quarter for that year.
Example: (this is what i got ... not right though)
Model:
DAX:
ByProductforselectedquarter =
VAR vTable =
SUMMARIZE (
sales,
Sales[Product], Sales[Sales] )
VAR Result =
SUMX ( vTable, Sales[Sales] )
RETURN
Result
how to get sales for the year until selected quarter for that year.
You can try something as below-
ByProductUptoselectedquarter =
var selected_max_date = max(Date[Date])
var selected_year = Year(selected_max_date)
return
calculate(
sum(Sales[Sales]),
filter(
all(Sales),
Year(Sales[Date]) = selected_year
&& Sales[Date] <= selected_max_date
)
)

Power BI. Using the SUMMARIZE function in a MAXX

I have table with Sales. All Sales divide between Men and Women. I need to find out who have the biggest count of sales, men or woman.
I have tried using Summarize and MAXX together, but sonething is wrong.
MAXX(SUMMARIZE(
'public Brand',
'public Brand'[Возрастная группа],
"Свод",
COUNT('public Brand'[Id]))
If I understand your requirement correct, you need a single output "Men" OR "Women" based on the count sales count/amount. For example, if there are total 10 count or row for Men and 12 for Women, you need Women as output from the measure. I prepared a very simple data set for example (Excel part in the image) to calculate the measure. You can see the final output in the red marked box.
Here below is the Measure Code-
For most number of sales number
Gender With Most Count =
VAR Gourp_by_gender_with_count =
SUMMARIZE(
Sales,
Sales[Gender],
"GenderCount", COUNT(Sales[Gender])
)
VAR max_count_among_gender_group =
MAXX (
TOPN(1,Gourp_by_gender_with_count,[GenderCount],DESC),
[Gender]
)
RETURN max_count_among_gender_group
For most number of sales amount
Gender With Most Sales Amount =
VAR Gourp_by_gender_with_amount =
SUMMARIZE(
Sales,
Sales[Gender],
"GenderWiseSales", SUM(Sales[Amount])
)
VAR max_amount_among_gender_group =
MAXX (
TOPN(1,Gourp_by_gender_with_amount,[GenderWiseSales],DESC),
[Gender]
)
RETURN max_amount_among_gender_group

DAX to calculate cumulative sum column (year to date) for all individual products

I have 3 columns in a table: Date, Product, and Volume. Based on this is I want to calculate the cumulative sum (or YTD) column for each of the products.
I know that to calculate cumulative total we use:
YTD_Actual = CALCULATE(Sum(Combined[Actual Volume]),FILTER(Combined,Combined[Date] <= EARLIER(Combined[Date])))
But I want this to additionally filter individual products and do this calculation for that product.
The expected column is shown in the picture below:
As far as you manage to get the Month column in date format, the following works:
YTD =
VAR currProduct = Table1[Product]
VAR currMonth = Table1[Month]
VAR ytdTotal =
CALCULATE(
SUM(Table1[Volume]),
FILTER(
ALL(Table1),
Table1[Month] <= currMonth &&
Table1[Product] = currProduct
)
)
RETURN IF(NOT(ISBLANK(ytdTotal)), ytdTotal,0)
Result: