Excluding null Value from Top 5 Rank but Including in Total - powerbi

So this is a one I have been cracking my head because I know there has to be a way.
I have a Top 10 Country column ranked based on sales. Unfortunately in my data source there were null values for countries, no value was defining it, and in order to fix those null values I replaced them with a string Unknown. The idea was to create Other category and everything not in the top 5 sum in there and finally have a total of all the values.
I started with ranking column defining,
Country Sales Rank = RANKX(
ALL (Country[Country Name]),
CALCULATE([Sales], ALLEXCEPT(Country,Country[Country Name])), ,DESC
The problem is when adding the rank to the countries the Unknown sales are top 4.
Unknown in the top 5
I also tried categorizing the countires in Other and then grouping them; one group for top 5 another Other group for the rest but the Unknown still remains in the top 4.
Country Top 5=
IF(
Country[Country Sales Rank] IN {1,2,3,4,5}, Country[Country Name], "Others")
Ideally this is what I am going for:
End result
Did anyone come across a similar issue I am having? Thanks!

Related

DAX & Power Query - Removing transaction line if sale is returned

First time posting, thanks in advance !
I have a simple sales table acting as a data source.
In this table I have all my dimensions and facts, the data model is not organized as a star schema.
I believe this question does not require the data model to be optimally built.
One column tracks the Sales Quantity and shows -1 if the item is returned.
In Power Query.
I wish, for all returns, to eliminate the line with the -1, but also the associated line with the +1 sale.
I do not wish to simply filter by the Invoice Number and remove it, as in the same invoice I may have other items who were not returned.
I wish to have in my final table only final records.
Thank you
Did not manage to identify a way to tackle the issue in Power Query as of yet.
Edit, addition of further context
Dataset abstract:
I would like to keep the green row and remove the yellow ones.
One invoice features three lines, two for one item being sold and returned, and one for an item being purchased for good. That latter one is to stay in the records.
[IMAGE : Excel version of relevant column in data base ]
https://i.stack.imgur.com/CiiiC.png
Because I do not know the exact nature of your data, I will give you a general solution for your problem, which will help you immensely.
First, I will transform your data into facts and questions. Then I will answer the questions depending on my experience analyzing many different datasets; last but not least, I will provide you with the solution.
Facts,
[Sales Amount Include Tax] column values for Return Sales are negative numbers because excel format negative numbers by including them with parentheses, as your data shows.
The Original Sale record and the Return Sale record must have the same values for these columns, which we will use to know the Original Sales record:
The absolute value for the [Sales Amount Include Tax] column.
[Invoice No], [Department], [Sub Dept], [Sub Dept GROUP], [Item Code] columns.
This point is critical because we will use these columns to know the Original Sales record.
Questions,
Is the Item code column value similar to the original sale row and the returned sales row?
My answer: It should be because you are returning the same item that the Item code column describes, but I am concerned that your data shows that the first two rows don't have the same Item code. Is it by mistake?
Is the Sales Quantity column always filled with -1, even if the original sales quantity was, for example, three pairs of shoes?
My answer: The Sales Quantity column should describe the number of items someone bought, so if I originally bought 2 things but didn't like them for a reason, I will return two items so that the column value will be -2.
If my above assumptions are correct, you need to do the following in the Power BI query:
1. Import your Sales table to Power BI. See this image that shows the test data I used.
2. When you import your data and see it in the Power Query Editor, you will see that the Sales Amount Include Tax column value shows minus rather than parentheses for Return Sales Rows, as you can see in this image.
3. Create a copy of your Sales table and Rename it to Returned Sales, then filter this table to include only [Sales Amount Include Tax] that are less than 0, as you can see in this image.
4. Filter the Sales table to include only [Sales Amount Include Tax] greater than or equal to 0, as you can see in this image.
P.S. Now comes the fun part :)
1. In Returned Sales table, add a custom column name it ReturnedSaleFlag, then give it a value of 1, as you can see in this image.
2. Change ReturnedSaleFlag column type to Whole number.
3. In Returned Sales table, transform [Sales Amount Include Tax] to an Absolute Value, as you can see in this image.
4. Now you need to return to the Sales table and merge it with Returned Sales table using multiple columns which are the column listed in the second point of the fact section, as the image shows. Also, see this link that describes how to Merge query based on multi columns.
5. Expanded the new Returned Sales column in Sales table and only select ReturnedSaleFlag column and make sure to remove the check from use original column name as a prefix, as the image shows.
6. Finally, all your previous hard work created a flag in your Sales table that will let you know the Sales record that had been returned. :)
7. Now you filter the Sales table to keep the records that only have null values for the new ReturnedSaleFlag column then remove this column, as the image shows.
8. In the end save and apply and analyze your Sales and your extra info in the Returned sales.
I hope I helped in a way; if so, please mark this as an answer and vote for it :)

Count Distinct ID if it has more than 7 periods

I am trying to counts students absent if they have skipped more than 8 bell periods on the same day. I was wondering if somebody could help me out here. I tried a distinct count with a counts of bell periods greater than 6 (that is the ballpark) but it is not working. I am providing a sample table below here.
You need to count the items, but using ALLEXECPT, to count the items by only using the filter context of StudentID and Date
So for example based on your data, assuming that student 101 missed 9 bells then:
Measure =
VAR _countofmisses = CALCULATE(COUNTAX(Table1, 1), ALLEXCEPT(Table1, Table1[Student ],Table1[Date]))
RETURN
IF(_countofmisses >=7, "Missed", "OK")
Which would give the following:
You can change the COUNTAX to a SUMX and still get the same result. All it is doing is counting/summing 1 for each row of the filter condition.
If I've read it wrong and student 101 has attended all 9 bells, just change the IF >= clause to the logic you need.

Create Calculated Column Based on Teams with Four Wins in Final Round Utilizing DAX

I need a calculated column for champions and runner up. To become a champion, a team must attain 4 wins in a given year in the final round. I just don't know how to translate that for DAX. I want to be able to have a Year Slicer that will show the Champion and Runner Up for a given year in a card.
I have tried a summary table and using TOPN, but since the count of finalists are all one, I am getting no luck. In the picture below, I would like a column delegating if a team is a champion or runner up
Create a new column by clicking in the Modeling tab and selecting 'New Column". Using the code below I created a new column which takes the column which contains the 'Count of Champions' and IF the number is greater than or equal to 4 then "Champion" is placed in the adjacent column, if the number is below 4, then "Runner Up" is placed in the adjacent column.
Status = IF(Table1[Count of Champions] >= 4, "Champion", "Runner Up")

Filter a measure in Power Pivot

I'm trying to group the far right column based on the (top 5/ all the rest) of the rank column. I need a result where I can display a chart that shows the aggregated 'number of sales' for the (top 5/all the rest)
Here is an example:
I'm using RANKX to rank based on the 'Units sold', and using an if statement to segment into a yes/no.
How do I aggregate this further so I can find the total units sold for YES and total units sold for NO? I need to keep this in the pivottable. I'm thinking of adding a measure that can be used as a filter or aggregation but I can't add measures to filter. I just started with DAX and I might be missing something.
If you just need a straight list of items with a total at the bottom, you could do this:
assume the table is called 'data':
--
Calculated Columns:
isTop5 = if(rankx(all(data),data[units])<=5,1,0) -- top five gets a 1, everything else 0
--
Measures:
numTopFive:=sum([isTop5]) -- add up your top fives (maybe your population only had 4 lines?)
numNotTop5:=count([code])-[numTopFive] -- subtract number of top 5s from total population count
--
-However-
If you try to subgroup this (i.e. group your codes into regions), then the example I provided will not show five top-5s for each subgroup, it will only show the sum of top 5s if a particular code so happened to be in the top 5 of the entire population. hopefully that makes sense. If you need this using sub-grouping I can provide a different example.

Aggregate the last value for child categories with DAX

I need to sum the values of stock on hand at a selected date, these values need to roll up into 2 or more categories.
The following measure gives me the on hand value at the lowest level item, but when it rolls up to the sub Category or Category level, it sums all the values and not the LASTNONBLANK per child item.
On Hand = CALCULATE(SUMX(Stock,[SOH]),FILTER('Calendar',[Date] <= LASTNONBLANK('Calendar'[Date],SUM(Stock[SOH]))))
My data set is similar to this:
Now to get the last SOH value where date <= 10 Dec 2017 works with my measure, with results like this.
As soon as I need to roll the aggregation up by removing the Size column the sum does not work as expected anymore.
This is what I need it to look like:
Similar to that I need the Category level to sum all the LASTNONBLANK values of the lowest level items, like this:
Any help would be appreciated.
I normally prefer to use measures rather than calculated columns in DAX but, because the relevant SOH values rely on a comparison the lowest level, this is a situation where I would add a calculated column.
Latest SOH:=
if(
CALCULATE(
LASTDATE('Stock'[Date]),
ALLEXCEPT('Stock',Stock[Category],Stock[Sub Category],Stock[Size])
)=Stock[Date],
'Stock'[SOH],
BLANK()
)
Starting from the middle and working out:-
ALLEXCEPT(...) is looking at all entries in the table that have the same value for Category, Sub Category and Size
CALCULATE(...) is then finding each of the last dates where there is a match
='Stock'[Date] is then checking to see if this row is the row with the highest date it can find with this Category/Subcategory/Size combination
If the values do match, then bring through the value of SOH, otherwise leave the cell blank.
Here's what it looks like in PowerPivot:
I wasn't quite sure what you wanted your dates to show. I've just added a measure that takes the LASTDATE of the date column:
And
I hope this helps!
Try to group by columns Category and Subcategory using sumx inside
GROUPBY (
Category,
SubCategory,
“SOH”, SUMX(expression)