How to find MAX of Summarized numeric column in Power BI - powerbi

I need to create a Measure to mark the row with 1 or 0 based on maximum count of a column Country. The country which has maximum count should be marked with 1 else 0. I am doing this in three steps. First finding count of all countries using SUMMARIZE function and then in second step I am finding the maximum value from the table created in the first step using SUMMARIZE function again. In the third step comparing the value of 2 step with count of each country, if they are equal then return 1 else 0.
Below is the DAX query which is giving wrong output :
CountOfState =
var t1 = SUMMARIZE(customer,customer[State Province],"CountOfStates",count(customer[State Province]))
var t2 = SUMMARIZE(t1,"countofstatess",maxx(t1,[CountOfStates]))
var v1 = CALCULATE(count(customer[State Province]),ALLEXCEPT(customer,customer[State Province]))
Return
if(v1 = t2,1,0)
--There is something wrong in var t2. t2 is giving wrong values because it is filtering out the values and I cannot figure out how to use ALL function for table expression.
Below is the wrong output :
Expected output :
As Texas has the highest count 3 this should be marked as 1 and others as 0.
Thanks in Advance.

Related

Power BI Calculate sum of column 2 when column 1 = " ? "

I want to create two measures that sum up the values of column 2 when column 2 equals column 1's parameters.
For example I have a column named Current/Reset and another column named Linear Ft. I want to sum up the values of Linear Ft where column 1 = "Current" and then again for "Reset."
I continue to get errors when I try and write the formula. Any help is much appreciated!
First of all you don't need explicit measures for this. It's the default behaviour of Power BI to sum up numbers (aka. implicit measure), and if you filter Linear Ft. by the (unique) values of Current/Reset you get your desired result.
However, if you still want to have separate measures for this, use CALCULATE()
Current Sum =
CALCULATE(
SUM('Table'[Linear Ft.]),
'Table'[Current/Reset] = "Current"
)
Reset Sum =
CALCULATE(
SUM('Table'[Linear Ft.]),
'Table'[Current/Reset] = "Reset"
)

Manipulate the total row to only sum the first row of every unique order in a list

This might be a strange question and not sure if it is possible to accomplish this with a Power BI table. My goal is to make the total row display the sum of the first row (Operation flag = 1) of every order in a list.
Logically the summarizing total row would produce the below result by summarizing all the rows in the "Manufactured Qty" column. (The below is just a manually created example in Excel to illustrate)
The desired DAX logic should instead produce the below result i.e. summarizing the quantity of every "Order no" with "Operation flag" equal to 1 in the total row.
Best regards,
Rubrix
One way to do this is to use a measure instead of the column. In this measure, use HASONEVALUE to check if the value is for a data row or for the totals row and either return the sum of all rows (for the normal case) or the sum of the rows where Operation flag = 1 (for the totals).
Manifactured Qty Measure =
var allRows = SUM('Table'[Manifactured Qty])
var firstOnly = CALCULATE(sum('Table'[Manifactured Qty]),'Table'[Operation flag] = 1)
RETURN IF(HASONEVALUE('Table'[Manifactured Qty]), allRows, firstOnly)

Power BI counting double values

I'm trying to create a measure in Power BI that will count doubles as a single value and then later add them all up to see how many doubles we have. Here is an example:
Each customer whose name shows up more than once should be counted as 1
Bonus question, how can I make a measure which will count customers whose name only shows up once (example name - Sarah).
Thanks in advance!
If you want to count the customers distinct you can use:
CountCustomers = DISTINCTCOUNT([Customer])
if you want to count the doubles, you can use:
Doubles = COUNTROWS(FILTER(SUMMARIZE(CusTable, CusTable[Customer], "countC", COUNTROWS(CusTable)), [countCol] > 1))
First I summarize it to a table with the name of the customer and how often it is appearing in the table
Next I filter this table by all rows bigger than 1
Last I count the rows
You can create this below measure to check the customer is there for once or multiple time in the list. This measure will return 1 if the customer is there for once and 0 if the customer exists for multiple time.
is_unique =
VAR current_customer = MIN(your_table_name[customer])
VAR customer_count =
CALCULATE(
COUNT(your_table_name[customer]),
FILTER(
ALL(your_table_name),
your_table_name[customer] = current_customer
)
)
RETURN IF(customer_count = 1, 1, 0)
This will return 1 for customer- Sarah and David. For all other customer, it will return 0. Now, if you add the above measure to a Card and apply SUM on the measure, it will return value 2 which is basically your customer count with single existence.

Measure with Sum using Calculate and IF not showing totals correctly in power bi

I have a requirement in power bi to display the amount value only for the minimum date selected by the user in the slicer. So created below measures..
For capturing the minimum selected range
StartDate = CALCULATE(min(‘DATE’[DATE]),ALLSELECTED(‘DATE’[DATE]))
For Displaying the amount only for the minimum date.
OP = if(SELECTEDVALUE(‘DATE’[DATE]) = [StartDate],
CALCULATE(sum(MEASUREMENTS[OPENING_BASE_VAL]),DATESBETWEEN(‘DATE’[DATE],[StartDate],[StartDate])),0)
I am getting desired output, but the grand-total for this measure becomes 0 as show in below image. Any help much appreciated.
You are getting 0 in the total because
SELECTEDVALUE(‘DATE’[DATE]) in your second formula returns blank, so your IF expression reads as " IF BLANK = [Start Date]", which is always false.
SELECTEDVALUE is a syntax sugar for the following code:
IF HASONEVALUE(DATE’[DATE]) then return Date[Date] else return blank. Since in the total you have many values (all dates), you are getting a blank.
To solve the issue, you need to iterate by dates.
OP =
VAR StartDate = [StartDate]
RETURN
SUMX(
VALUES(Date[Date]),
IF(Date[Date] = StartDate, CALCULATE(SUM(MEASUREMENTS[OPENING_BASE_VAL])), 0))
Here, we first save start date into a variable to avoid calculating it multiple times. Then we create a list of dates using VALUES function, and use SUMX to iterate this list, date by date. If currently iterated date is a start date, then values are summed, otherwise you get zero.
If instead of zeros you can use blanks, you can use a more optimal/faster code:
OP =
VAR StartDate = [StartDate]
RETURN
CALCULATE(SUM(MEASUREMENTS[OPENING_BASE_VAL]), KEEPFILTERS(Date[Date] = StartDate) )
Here, we calculate sums only where dates are equal the start date. Since there is no iteration, and there is no "IF", the code is much faster. The total will be the same, only line items will show blanks instead of zeros.

PowerBi Change Card values to previous month Value if Current month value is not available

I am working on powerbi, using data over 2 months period, writing dax queries and trying to build reports.
I am trying to show the monthly values in cards visuals.
And i am able to do it using the measure below.
Sample Data:-
PlanPrevMon = CALCULATE([PlanSum],PREVIOUSMONTH('Month Year'[Date]))
CustomKPI = IF(ISBLANK([PlanSum]),"No Data Available ",[PlanSum])&" "&IF([PlanSum]=[PlanPrevMon],"",IF([PlanSum] > [PlanPrevMon],UNICHAR(8679),UNICHAR(8681))&IF([PlanSum]<=0,"",""))
But here i don't want user to choose the month values from slicer.
I would like to show the card values as
if current month value is not available then it should compare with the previous month value automatically as shown in an image below
For example, Apr-2017 value is not available; in this scenario I would like it to compare with the Mar-2017 value.
If Mar-2017 value also not available, then the previous month value and so on
Edit
I tried what #user5226582 suggested but still getting wrong values as below image.
As you can see i restricted to crosscheck whether the value is getting in a right way of not.
But not.
This is the measure i have used as #user5226582 suggested.
c =
var temp = 'Revenue Report'[Date]
return CALCULATE(LASTNONBLANK('Revenue Report'[Planned Rev],
'Revenue Report'[Planned Rev]),
ALL('Revenue Report'),
'Revenue Report'[Date]<=temp)`
Can you please correct me if i am doing anything wrong
Does this help...?
To get the PlanPrevMon column to show like this, I first created a new index column:
id = COUNTROWS(FILTER(Sheet1, EARLIER(Sheet1[Date],1)>Sheet1[Date]))
Then I used the index to help create the PlanPrevMon column in two steps:
Step 1: I made one column named PlanPrevMon1.
PlanPrevMon1 = SUMX(FILTER(Sheet1,Sheet1[id]=EARLIER(Sheet1[id])-1),Sheet1[PlanSum])
Step 2: I made another column named PlanPrevMon.
PlanPrevMon = if(ISBLANK(Sheet1[PlanPrevMon1]),
if(Sheet1[id]=1,0,CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon1],Sheet1[PlanPrevMon1]),ALL(Sheet1),ISBLANK(Sheet1[PlanSum]))),
Sheet1[PlanPrevMon1])
For the card, I used this measure:
Card = CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon],1),FILTER(Sheet1,Sheet1[id]=max(Sheet1[id])))
I hope this helps.
You can use LASTNONBLANK DAX function.
Example data:
a b
1 1
2
3 2
4
5 3
Calculated column:
c =
var temp = Table1[a]
return CALCULATE(LASTNONBLANK(Table1[b],Table1[b]),ALL(Table1),Table1[a]<=temp)
Results in:
a b c
1 1 1
2 1
3 2 2
4 2
5 3 3