Running MAX of values in another column in DAX - powerbi

I'm working on having a column whose values are the running MAX of another column.
My main table has three columns and I use the summarize function to virtually add another column named as SUM OF EACH DATETIME to the summarized table. Now I want to have the running MAX of the SUM OF EACH DATETIME column in the summarized table in another new column as MAX of Sum column. My table and its preferred columns are shown below:
I'd appreciate it if you kindly guide me how can I have the MAX of Sum column in my summarized table.
I should note that the formula to calculate the SUM OF EACH DATETIME column is:
SUMMARIZE(TABLE, TABLE[DateTimeStamp],
"SUM OF EACH DATETIME", IF(COUNTROWS(TABLE)=calculate(DISTINCTCOUNT(TABLE[Name]), ALLSELECTED()),SUM(TABLE[Value]),BLANK()))

You can create one measure and one column as given below.
Column:
date_sum_column =
var current_row_date_time = ('your_table_name'[DateTimeStamp])
return
CALCULATE(
SUM('your_table_name'[Value]),
FILTER(
ALL('your_table_name'),
'your_table_name'[DateTimeStamp] = current_row_date_time
)
)
Measure:
running_max_sum =
CALCULATE(
MAX(your_table_name[date_sum_column]),
FILTER(
ALL(your_table_name),
your_table_name[DateTimeStamp] <= MIN(your_table_name[DateTimeStamp])
)
)
Here is the output:

Related

Dax measure (add new columns) not showing total in table

I use DAX to add two new columns "netWeight_Shipped" and "NetWeight_notShipped" based on existing column "NetWeight_Total" and groupped by "BatchNo" and filtered by OutStockTransactionID as below:
--New column
NetWeight_Shipped =
CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] <> 0
)
--New column
NetWeight_notShipped =
CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] = 0
)
Then put those columns on table as the screenshot. However, two new columns not showing total values in table.
What should I change to have total values for those new columns?
In order to display total values in the table, you should create and use two new measures "netWeight_Shipped" and "NetWeight_notShipped" based on the existing columns, but not two new columns.
-- New measure
NetWeight_Shipped = CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] <> 0
)
-- New measure
NetWeight_notShipped = CALCULATE(
SUM(Fact_ShippingKPI[NetWeight_Total])
,ALLEXCEPT(Fact_ShippingKPI,Fact_ShippingKPI[BatchNo])
,Fact_ShippingKPI[OutStockTransactionID] = 0
)
You should apply aggregation to your column to see the total value below. Look at this image I have added the same column twice and for the first one, there is no total value. But for the second one, there is a total. This is only for the aggregation I applied to the second column but not to the first column-

Counting distinct IDs for each date in Power BI

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

Power BI DAX measure calculation

Step1:
I have created a calculated table containing Level, Location, L2code from level sales data. I need to create a report that will count rows based on the level1 group.
Note that there are more levels in the table. This is only an example of one of the levels.
How can I count the rows for each level1?
Step2:
I need to create all combinations of counts based on location and l2 code and count the numbers.
like in example 2 location and 8 distinct l2code so it should be 2*8 =16 total possible rows.
How can I achieve this using the DAX measure?
Source data file
Output report
first one is a simple measure as below-
DistinctRowCount = Count(your_table_name[Level1])
Second one is bit tricky and you can try this below measure-
CrossCount =
var distinct_location =
calculate(
distinctcount(your_table_name[Location]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location]
)
)
var distinct_l2code =
calculate(
distinctcount(your_table_name[l2code]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location],
your_table_name[l2code]
)
)
return distinct_location * distinct_l2code
Sample output-

DAX sum filtered by multiple columns of related tables

I have a measure, which is being added to a table and a Card. The measure is used to show the total hours posted where Calls.[Sch Engineer]=Hours.Employee AND Calls.ProjID=Hours.ProjID
The value on each row of the table is correct. However the total value for this measure is incorrect in the table and also the Card, and I cant work out why.
So the measure is:
SchEngHrsOnly =
VAR main =
MAX ( Calls[Sch Engineer] )
RETURN
CALCULATE ( SUM ( 'Hours'[Hrs] ), Hours[Employee] = main )
When I add this to a table visual I get the following - both Table total and Card value for measure are incorrect - they should be 163.50:
If I select a row in the table visual, then the Card visual shows a correct value, otherwise it shows an incorrect value:
My data model is:
My relationships are:
What I am looking to get is:
Also, please see here the PBIX file:
PBIX File
Can anyone help with this issue please?
If you want SUM of MAX values, then go this way:
SumOfMaxes =
SUMX(
VALUES( Hours[ProjID] ),
CALCULATE( MAX( Hours[Hrs]) )
)
It produces:
You might be also interested in:
DAX ALLEXCEPT to sum by category of multiple dimension tables
DAX Median of category sums
Edit
After your explanations I see that you want filtered sum.
FilteredSum =
CALCULATE (
SUM ( Hours[Hrs] ),
FILTER (
Hours,
Hours[Employee] = RELATED ( Calls[Sch Engineer] )
&& Hours[ProjID] = RELATED ( Calls[Proj ID] )
)
)
https://www.sqlbi.com/blog/marco/2010/02/09/how-to-relate-tables-in-dax-without-using-relationships/
You can create it as a calculated column and then sum it up to get the value:
Hours Calc = CALCULATE(MAX(Hours[Hrs]),FILTER(Hours,Hours[ProjID]=Calls[ProjID]))
The above calculation will add a column with the maximum of hours for each project ID. Then you can sum it up in the final table and should get the desired results. Hope this helps.

Power BI Sum cells grouped by same ID

How can I make a formula which sums all the cells which have the same ID?
Edit: Both of these columns are in the same table. I need a new column which calculates the sum of all the hours with the same ID.
In that case, I would create a new summery table:
SummeryTable =
SUMMARIZE(
'TableName';
'TableName'[ID];
"Time_summed"; SUM('TableName'[TimeColumn])
)
Then you will end up with a table with distinct [ID] on each row and the sum of all hours corresponding to this ID from the original data table. Which you can use to create a relationship with the original table.
EDIT_1:
Yes you can if you use a calculated column like this:
SumTime =
VAR thisID = [ID]
RETURN
CALCULATE(
SUM('TableName'[TimeColumn]);
ALL('Tablename');
'TableName'[ID] = thisID
)