MDX query for percent of current row within table - powerbi

I have an SSAS cube with a FactTicket, that has a TicketsSold measure.
The cube has a good number of dimensions. Lets call them Date, Agent and Geo.
I can create a table (we're using Power BI service, if it makes a difference) and put in it all and any dimensions, and the TicketsSold measure.
What I'd like to do, is have another measure that'll calculate the percent of each row in the table out of the total of the table at that moment.
so suppose I have the following data:
Date Agent Geo TicketsSold
2020-04-01 Moulder US 12
2020-04-01 Moulder UK 4
2020-04-02 Moulder US 10
2020-04-03 Moulder UK 5
2020-04-01 Skully US 16
2020-04-01 Skully UK 12
I would like to be able to filter my report on any measure, display any other measure(s) with the TicketsSold and the percent.
Such as, filtered on 2020-04-01 and 2020-04-02:
Agent Geo TicketsSold %ofTicketsSold
Moulder US 22 40.74 %
Moulder UK 4 7.40 %
Skully US 16 29.63 %
Skully UK 12 22.22 %
TOTAL 54 100 %
Or filtered only on Agent Moulder:
Date TicketsSold %ofTicketsSold
2020-04-01 16 51.61 %
2020-04-02 10 32.26 %
2020-04-03 5 16.13 %
TOTAL 31 100 %
It's clear to me that what I probably need is some way of getting -- for each row in the table -- the total of the table itself, and then dividing the row's own TicketsSold by this total.
But I can't seem to figure out a trivial way of creating such a calculation. Is this doable, without having to define a tuple of all possible dimensions?
Thanks!

You can achive that using the axis function . Below is an example based on Adventureworks
where we will create a PercentageOFTotal Column based on InternetSales
select
{[Measures].[Internet Sales Amount]}
on columns,
non empty {([Product].[Category].[Category],[Product].[Subcategory].[Subcategory])}
on rows
from
[Adventure Works]
where [Date].[Calendar Year].&[2013]
Result
Now lets add our new column
with member measures.PercentageOFTotal
as round(([Measures].[Internet Sales Amount]/sum(Axis(1),[Measures].[Internet Sales Amount]))*100,2)
select
{[Measures].[Internet Sales Amount],measures.PercentageOFTotal }
on columns,
non empty {([Product].[Category].[Category],[Product].[Subcategory].[Subcategory])}
on rows
from
[Adventure Works]
where [Date].[Calendar Year].&[2013]
Result

If you are already using PBI: why don't you make use of DAX? A DAX query is able to solve your problem: MEASURE Fact[Percent] = CALCULATE(SUM(Fact[Amount]), ALLSELECTED(Fact)). "ALLSELECTED" is similar to VISUALTOTALS() in MDX, but more dynamic.

Related

How to group by on Power BI using DAX

I have two tables in Power BI as follows:
COUNTRIES
COD COUNTRY
1 BRAZIL
2 ARGENTINA
3 CHILE
4 BRASIL
5 COLOMBIA
6 ARGENTINA
7 URUGUAI
SALES
COD DATE
1 2021-01-02
2 2021-10-01
3 2019-09-04
1 2018-07-05
7 2019-04-10
There's a relationship between the two tables, on the COD column.
I need to count how many countries (column "COUNTRY" from the table "COUNTRIES") have a status CHURN. It's considered CHURN when their latest order from the table "SALES" is more than 180 days, using today as a reference.
I know that I need to group by the MAX date per country, do a DATEDIFF, and then do a COUNT. I've tried using ALL and SUMMARIZE, but I haven't found a way to solve this problem.
Are you able to add a calculated column to store the max sales date for each country in your COUNTRIES table? Either in Power BI or directly in your database. If so, here's one solution with 2 steps.
Create a MaxSalesDate column in your COUNTRIES table. DAX for a calculated column below:
MaxSalesDate =
VAR COD = COUNTRIES[COD]
RETURN MAXX(FILTER(SALES, SALES[COD] = COD), SALES[DATE])
Create a measure that counts the number of MaxSalesDate values that are older than 180 days old:
CountCHURN = COUNTX(COUNTRIES, IF(DATEDIFF(COUNTRIES[MaxSalesDate], TODAY(), Day) > 180, 1))

How to remove or exclude rows from a table using date slicer selection?

As per the below scenario,I need to exclude all the rows in the table Sales with columns Client,sales,salesdate and region which fall outside the selected maximum and minimum date range of the date slider.
Could anyone please suggest a DAX , I will share my DAX query which is not working.I tried to search official guides in PowerBI for selectedValues() to handle this date filter condition but nothing seems to be working.
I created a Calendar table with distinct values of the dates as present in the Sales Dates column of the Sales table. There is no relationship between these two tables. I used the below DAX measures to handle the filtering criteria for excluding the rows:-
Below measures are meant to find maximum and minimum dates in the selection slider
MaxDate=MAX(DateDimention[Dates])
MinDate=MIN(DateDimention[Dates])
Below ExcludedMeasure is meant to set 1 flag to the values other than selected values which are selected for both min and max dates.
I dragged and dropped it in the visual filter of my table Sales and set it to dont show.But it seems to be not working.
Please suggest how can I fix it.
ExcludeMeasure = IF (SELECTEDVALUE(Calendar,Calendar[MinDate],0,1),
IF (SELECTEDVALUE(Calendar,Calendar[MaxDate],0,1)
My Present Visualization and independent Calendar table to be used in the filter slider:-
Present visualization
Independent Calendar Table
Input data source [Excel]
Client Sales SalesDates Region
A 1000 1.1.2000 USA
A 100 1.2.2000 USA
A 200 4.3.2000 USA
B 110 4.3.2000 Europe
B 1000 5.4.2000 Europe
B 200 6.8.2001 Europe
C 1100 7.9.2001 Asia
C 2000 8.12.2001 Asia
D 100 1.2.2002 Australia
D 1300 6.3.2002 Australia
E 100 7.5.2002 Africa
Expected Results :
If I select Minimum slider date as 01.04.2001 and maximum slider date as 1.09.2001 in my[Dates] filter visualization, then it should return the below results after excluding '5.4.2001' and '6.8.2001':-
Client Sales SalesDates Region
A 1000 1.1.2000 USA
A 100 1.2.2000 USA
A 200 4.3.2000 USA
B 110 4.3.2000 Europe
C 1100 7.9.2001 Asia
C 2000 8.12.2001 Asia
D 100 1.2.2002 Australia
D 1300 6.3.2002 Australia
E 100 7.5.2002 Africa
Kind regards
Sameer
A relationship between the date table and the sales table would be ideal, as when you filter the date from the date table, the filter would exclude the records from the sale tables.
If you can't have a date table with the relationship, your exclude measure needs to be something like this:
Exclude =
CALCULATE(COUNT(Sales[Id]),
Sales[SalesDates] >= [MinDate] &&
Sales[SalesDate] <= [MaxDate]) > 0

Calculate total variance in power bi with dax

I have 3 measures:
1) total_trx = SUM(mytable[trx])
2) trx_prev_month = CALCULATE([total_trx], DATEADD(calendar[date], -1,MONTH))
3) monthly_var = DIVIDE([total_trx],[trx_prev_month])-1
If I add a waterfall viz, x-axis with month, it gives me the % of monthly variation and a TOTAL bar at the end that sums all the variations.
I need to reproduce that total number in order to show a KPI as in "so far, we've increased ...%", changing when using a date slicer.
Seems like sum(monthly_var) is not allowed.
Any ideas?
Thank you very much.
Edit1: sample with date filter = Last 4 months
Jul 100 0%
Aug 110 10%
Sep 90 -20%
Oct 80 -10%
Total: -20% <- need a dax to calculate this number and show just -20%
Then if I change the filter to, for example LAST 6 MONTHS, I need to calculate it up to May
In order to get the desired result we will use an intermediate table in our query that will summarize the results by months:
use this code and replace calendar[Year Month] with your Year month column :
SUMX(
SUMMARIZECOLUMNS(calendar[Year Month],"Monthly_int_var",[monthly_var]),
[Monthly_int_var]
)

How to multiple two columns in a row with max date in PowerBi?

This is my first PowerBi report. 
I've a table structure like this
TransDate UnitsAsOf Price InvestedAmount Stock
01/02/2020 10 12.4 124 APL
01/03/2020 20 13 260 APL
01/05/2020 21 15 315 APL
01/10/2020 1 111 111 BPL
And this is the table Visualization I'm creating
Stock Total invested (Summarized column) Current Value
APL 699 THIS IS A MEASURE column
BPL 111
I just couldn't figure out how to get max(transDate) for each stock and multiple it with the Price of that row?
Any help please?
You can do it like this
measure =
SUMX(TOPN(1, YourTable, YourTable[TransDate], DESC), YourTable[Price] * YourTable[InvestedAmount])
the TOPN will return you the row with the latest date, and in the SUMX you use the fields from that row.
OPTION-1
You can simply create this following measure-
max_date = MAX(your_table_name[TransDate])
Now add a table visual with column - Stock, InvestedAmount (Default SUM applied) and New Measure max_date. The output will be as below-
OPTION-2
You can also add all 3 column - Stock, InvestedAmount and TransDate directly and select Latest for TransDate as shown below and the output will be same-

DAX Measure or Calculated Column from Slicer Value

I have a table with customers' SSN, account number, purchase date, and max purchase date (the most recent purchase date for SSN, across all the accounts). Customers can have multiple accounts. I know how to create a measure the calculate the distinct count of all the accounts that haven't been active since a certain date (6 months, 18 month, 24 months)..
I would like to create a measure or a calculated column to give me the following information.
when users select the date from the slicer (say 6 months) the chart shows the count of the accounts that have not made a purchase in 6 months, the users also want to have a drop down slicer("Yes", "No") to indicate if the SSN had activities under other accounts. i.e. if the max purchase date is greater than the value from the date slicer.
the table structure looks like this:
SSN AccountNumber LastPurchaseDate MaxPurchaseDate
123-45-5678 9876 8/2/2018 9/4/2019
123-45-5678 6398 9/4/2019 9/4/2019
135-65-4321 2233 6/6/2019 6/6/2019
Best way here would be if you add a custom column with the time difference (in the query designer):
= [MaxPurchaseDate] - [LastPurchaseDate]
Now you have something like this:
SSN AccountNumber LastPurchaseDate MaxPurchaseDate DateDiffDays
123-45-5678 9876 9/2/2018 9/4/2019 2
123-45-5678 6398 9/4/2019 9/4/2019 0
135-65-4321 2233 6/12/2019 6/6/2019 6
You can add another column which acts as filter for your 6 months, 18 month, 24 months (convert the DateDiffDays into months).
The following measure counts the accounts:
=Distinctcount('YourTable'[AccountNumber])
If you filter now by your 6 months, 18 month, 24 months column the measure gets after every filtering calculated again and you get your result.