Counting distinct IDs for each date in Power BI - powerbi

I have a dataset and I want to create a column(not measure) to calculate the count of customers in each month. I don't know how I can count each customer once a month in Power BI.
I wrote this code but it counts the number of frequent customers more than once a month.
myCol = CALCULATE( DISTINCTCOUNT('table'[user_id] ) , 'table'[order_date] )
For example, it's my data:
The true result should be:
but my code returns this result:
How should I write the code for this calculating column to get a true result?

Since you are trying to calculate per month, you need a "year_month" column.
Then:
count_of_customer =
CALCULATE(
DISTINCTCOUNT('table'[user_id]),
ALLEXCEPT('table', 'table'[year_month])
)
Result:
Edit:
You don't need a calculated column, you need a measure:
count_of_customer =
COUNTROWS (
SUMMARIZE ( 'table', 'table'[year_month], 'table'[user_id])
)

Related

DAX - Get list from a filtered SUMMARIZE formula

So, I have the following tables in my Power BI :
Sales : Date | ID_Client | ID_Product | Amount
Client : ID_Client | Name_Client
I would like to get the number of unique BIG clients in any given month. I therefore use the following formula (which I then put in a column in a table with months in rows):
# BIG Clients =
VAR threshold = 10000
RETURN
(
CALCULATE(
DISTINCTCOUNT( Sales[ID_Client] ),
FILTER(
SUMMARIZE(
Sales,
Sales[ID_Client],
"Sales", SUM( Sales[Amount] )
),
[Sales] >= threshold
)
)
)
QUESTION IS : how can I get the list of those BIG clients for any given month? Let's say I click on the November number of big clients in my table, could another table nearby display the list of those clients ?
Thanks in advance for your kind help, I've been trying for a while :)
I assume that you have a table of clients with the Name column with a one to many relationship with the Sales table and that you do not have duplicate client names. Then you may create a [BIG Sales] measure to be used in a table or matrix visual with client names on the rows.
since [BIG Sales] evaluates to BLANK() for clients with less that threshold sales, they are automatically filtered out from the visual
BIG Sales =
VAR threshold = 10000
VAR BigCustomers =
FILTER(
ADDCOLUMNS(
VALUES( Clients[Name] ),
"Sales", SUM( Sales[Amount] )
),
[Sales] >= threshold
)
RETURN
SUMX(
BigCustomers,
[Sales]
)
You could create a table or matrix visual with the client on the rows and use your measure in the values field. This will show 1 for all big clients and return blank for the rest (which should hide them). If you don't want to show the measure, you can set the value is 1 in the filter pane and remove the measure from the values field.
A more direct option is to use a simple SalesAmount = SUM ( Sales[Amount] ) measure in the values field and filter like this

Power Bi dax measure help: tips on ignoring a slicer

I am working in Power BI and I created a DAX measure that adds up two other DAX measures.
For one measure I need it to ignore the month slicer because I need the total for that category. Is it possible to do so?
Also, is it possible for it to ignore the slicer and still give me the total for unfiltered DAX measure + the date filter DAX measure?
DAX code:
Monthly Total Act =
CALCULATE (
SUM ( 'sheet1'[Amount] ),
FILTER ( 'sheet1', 'sheet1'[Type] = "ACT" ),
FILTER ( 'sheet1', 'sheet1'[Bill] = "Y" )`
)
Monthly Total of Acs =
CALCULATE (
SUM ( 'sheet1'[Amount] ),
FILTER ( 'sheet1', 'sheet1'[Type] = "ACR" ),
FILTER ( 'sheet1', 'sheet1'[Bill] = "Y" )`
)
Adding these two formulas together to get the total monthly.
The monthly total of ACS is where I encounter the problem. I need this to be unaffected by the slicer
The end goal is having the month total of ACS unaffected by the data slicer and add to the monthly total of Act that requires filter by the current month.
Yes, you can add this line as a third filter argument in the calculat function you want to ignore the month slicer:
ALL('tableName'[monthColumnUsedAsSlicer])
Then the monthe slicer will not affect calculations.

Calculate cumulative sum of summarized table column

I having trouble calculating the cumulative sum of a column on PowerBI.
I have a big offer table and I want to run a pareto analysis on it. Following many tutorials, I created a SUMMARIZED table by offer and a sum of their sales. So the table definition is:
summary = SUMMARIZE(big_table; big_table[offer]; "offer sales"; sum(big_table[sales]))
Many of the forums and stackoverflow answers I found have direct me to the following formula for cumulative sum on column:
cum_sales =
CALCULATE(
sum([offer_sales]);
FILTER(
ALLSELECTED(summary);
summary[offer_sales] <= max( summary[offer_sales])
)
)
However the resulting table is not correct:
What I need is simply to have the offers ordered by sales descending and then add the current row's sales amount to the previous row's sales,
So I excepted numbers closer to:
1st row: 1.5M
2nd row: 2.1M
3rd row: 2.6M and so on
But (maybe) because of my data structure and (certainly) lack of knowledge on how PowerBI works, I'm not getting the right results...
Total Amount = SUM ( 'Fact'[Amount] )
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", [Total Amount]
)
VAR CurrentOfferAmount = [Total Amount]
VAR OffersLessThanCurrent =
FILTER (
OfferSum,
[amt] <= CurrentOfferAmount
)
RETURN
SUMX (
OffersLessThanCurrent,
[amt]
)
There's no need to pre-aggregate to a summary table. We can handle that as in the measure above.
This assumes a single fact table named 'Fact', and a table of distinct offers, 'Offer'.
Depending on what you're doing in terms of other filters on 'Offer', you may need to instead do as below:
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", CALCULATE ( [Total Amount], ALLEXCEPT ( 'Offer', 'Offer'[Offer] ) )
)
...
The rest of the measure would be the same.
The measure is fairly self-documenting in its VARs. The first VAR, OfferSum is a table with columns ('Offer'[Offer], [amt]). This will include all offers displayed in the current visual. CurrentOfferAmount is the amount for the offer on the current row/axis label of the visual. OffersLessThanCurrent takes OfferSum and filters it. Finally, we iterate OffersLessThanCurrent and add up the amounts.
Here's a sample:

Calculate price based on distinct count

I am having trouble working out a measure (Revenue) in power bi.
I have a measure which is basically counting distinct values in a table (table 1). From this column I want to multiply the distinct count to get the total price (prices are in another table).
See below for an example
Table 1
Product DistinctCount Revenue (Measure I am trying to Calculate)
A 15 45.00
B 30 60.00
Prices Table
Product Price
A 3.00
B 2.00
At the moment the Revenue is calculating based on COUNT and not DISTINCTCOUNT.
Any help would be much appreciated.
thanks!
Measures, Calculated Columns, Google
I am assuming you have a relationship set up between these two tables on [Product]. If this is the case you can do something like this to create a calculated column:
Revenue =
CALCULATE (
SUMX ( 'Table 1', 'Table 1'[DistinctCount] * RELATED ( 'Prices Table'[Price] ) )
)
If you are trying to create a table visual try the DAX below, where ID is just a transaction ID for each product in your 'Table 1':
Revenue =
VAR DistinctCountOfProductTransactions =
CALCULATE ( DISTINCTCOUNT ( 'Table'[Id] ) )
VAR Result =
CALCULATE (
DistinctCountOfProductTransactions * SUM ( Prices[Price] ),
TREATAS ( VALUES ( 'Table'[Product] ), Prices[Product] )
)
RETURN
Result

How to calculate Cumulative Sum in Power BI

I know how to do cumulative sum if dataset has Dates, But I am struggling to do same if I do not have dates in my dataset.
Below is the data, I want CUM Sales
I've selected New quick measure -> Totals -> Running total and creates this:
sales running total in part =
CALCULATE(
SUM('Query1'[sales]);
FILTER(
ALLSELECTED('Query1'[part]);
ISONORAFTER('Query1'[part]; MAX('Query1'[part]); DESC)
)
)
Returns:
You can also create own measure to calculate cumulative sum.
Select New Measure from the ribbon and write the following expression
Cumm Sales =
VAR Current_Part = MAX(Test[Part])
RETURN
CALCULATE(
SUM(Test[Sales]),
Test[Part]<=Current_Part,
ALL(Test[Part])
)
Output: