Advanced Filter on PowerQuery - powerquery

Question
I am trying to filter some excel data I have on PowerQuery but am really struggling to figure out the best way to do this. On Excel I would usually do this using an Advanced Filter but I don't believe Power Query has the same functionality.
I would like to filter across a number of columns, using an OR condition for a:
a range of values in some columns; and
a range of 'wild card' searches in other columns
Example
As an example if my dataset is as below
Name
Function
Sub-Function
Position
Country
Andy
Sales
Omni-Channel
Sales Manager
Brazil
Bob
Marketing
eCommerce
Web Design
Argentina
Rakesh
HR
Business Partnering
HRBP
Italy
Tom
Finance
Reporting
Finance Manager
UK
Chris
Sales
Trade Marketing
Sales support
US
Raj
Legal
Legal
Para-legal
Brazil
I might want to filter for anyone meeting the following criteria;
working in the sales function OR
working in the Trade Marketing or Omni-Channel sub-function OR
has the words 'Manager' or 'Sales' in their Position title OR
is based in Brazil
The desired output would then be
Name
Function
Sub-Function
Position
Country
Andy
Sales
Omni-Channel
Sales Manager
Brazil
Tom
Finance
Reporting
Finance Manager
UK
Chris
Sales
Trade Marketing
Sales support
US
Raj
Legal
Legal
Para-legal
Brazil
My current approach
My approach was to create a table which had all my criteria (not including the 'wild card' searches), upload to PowerQuery, create multiple lists from this table (called for example Filter1, Filter2 etc..). Then using the following formula against my main dataset
= Table.SelectRows(#"Filtered Rows1", each (List.Contains(Filter1,[Function]) = true) or (List.Contains(Filter2,[Sub-Function]) = true) or (List.Contains(Filter3,[Country]) = true) or Text.Contains([Position], "Manager") or Text.Contains([Position], "Sales"))
Issues
The formula above works for really small data sets however not on my 80,000 line data set, or rather it does not work within a reasonable time frame
I have a long list of 'wild card' searches which apply to three columns so typing in the Text.Contain(...... etc.. formula multiple times into the formula seems very inefficient, is prone to mistakes and is not dynamic in any way.
I'm sure there must be a better way to do this but I have not found many helpful discussions or tutorials on this online so am reaching out to the community.
Thank you

I think the part of the problem the query is slow as with your current approach, filtering is evaluating filtering conditions separately before it could filter. Power bi is capable of handling filter on multiple columns in a single filter, like following
Table.SelectRows(
#"Changed Type1",
each ([Function] = "Sales")
or ([#"Sub-Function"] = "Trade Marketing" or [#"Sub-Function"] = "Omni-Channel")
or (
Text.Contains([Position], "Manager")
or Text.Contains([Position], "Sales")
or ([Country] = "Brazil")
)
)

Related

Deactivating a filter applied to a card in POWER BI

Rank Turnover =
RANKX(
ALL(Regions[Region]),
CALCULATE([Sales Total],KEEPFILTERS(Sales[Currency]="EUR"))
)
Hi everyone, got stuck with this one for the second day in a row. There are 3 tables in a dataset (Regions, Sales and Currency). Regions and Currency are filtering out the Dashboard when applied. My Intention is to have Total Sales in a Card for EUR only for a Chosen in a Region Filter Country. Everything works fine, but whenever I choose "LC" (Local Currency) in Currency Filter, the numbers Change - I Need to overcome that. Changing LC in that Filter should not affect Total Sales, they have to still be done in EUR.
Any help will be appreciated,
Thank you
Rank Sales EUR =
RANKX(
ALL(Regions[Region]),
CALCULATETABLE(
{[Sales Total]},
TREATAS({"EUR"}, 'Dashboard Currency'[Currency]),FILTER(Dates, Dates[Year]=MAX([Year]))))
Here is the answer. Thank you all for support

dax handling summary values in a matrix report

Source data columns are Store, Product, StoreSales, and ProductSales
StoreSales has duplicate values, even across different Stores.
Looking for a dax measure to handle StoreSales as described in the image.
This is the closest so far, but doesn't account for duplicates between stores.
Store Sales:=
sumx(DISTINCT(_Sales[StoreSales), _Sales[StoreSales])
screenshot of source and pivot table
To answer your question directly, this formula should give you the desired result:
Desired Result for Store Sales =
IF(ISFILTERED(_Sales[Store]), SUM(_Store[Store Sales]))
However, I recommend to revisit your data model design. It's conceptually incorrect (you are mixing detailed data with the summary of the same data), and you will have serious problems with DAX. A better way to structure your data:
Remove column "Store Sales". It's redundant and does not fit the data level of detail.
Rename column "Product Sales" into "Sale Amount". It's just sale amount, without any qualifiers.
Create a measure "Total Sales" = SUM(_Sales[Sale Amount]). It will correctly calculate total sales both on product and store levels.
If you need a special measure for store sales, use SUMX:
Store-level sales = SUMX (VALUES(_Sales[Store]), [Total Sales])
And if you need to show product contributions to store sales:
Product Contribution = `DIVIDE([Total Sales], [Store-Level Sales])

DAX COUNT/COUNTA functions

I've looked at many threads regarding COUNT and COUNTA, but I can't seem to figure out how to use it correctly.
I am new to DAX and am learning my way around. I have attempted to look this up and have gotten a little ways to where I need to be but not exactly. I think I am confused about how to apply a filter.
Here's the situation:
Four separate queries used to generate the data in the report; but only need to use two for the DAX function (Products and Display).
I have three columns I need to filter by, as follows:
Customer (Display or Products query; can do either)
Brand (Products query)
Location (Display query)
I want to count the columns based on if the data is unique.
Here's an example:
Customer: Big Box Buy;
Item: Lego Big Blocks;
Brand: Lego;
Location: Toys;
BREAK
Customer: Big Box Buy;
Item: Lego Star Wars;
Brand: Lego;
Location: Toys;
BREAK
Customer: Big Box Buy;
Item: Surface Pro;
Brand: Microsoft;
Location: Electronics;
BREAK
Customer: Little Shop on the Corner;
Item: Red Bicycle;
Brand: Trek;
Location: Racks;
In this example, no matter the fact that the items are different, we want to look at just the customer, the brand, and the location. We see in the first two records, the customer is "Big Box Buy" and the brand is "Lego" and the location is "Toys". This appears twice, but I want to count it distinct as "1". The next "Big Box Buy" store has the brand "Microsoft" and the location is "Electronics". It appears once and only once, and thus the distinct count is "1" anyway. This means that there are two separate entries for "Big Box Buy", both with a count of 1. And lastly there is "Little Shop on the Corner" which appears just once and is counted just once.
The "skeleton" of the code I have is basically just to see if I can get a count to work at all, which I can. It's the FILTER that I think is the problem (not used in the below example) judging by other threads I've read.
TotalDisplays = CALCULATE(COUNTA(products[Brand]))
Obviously I can't just count the amount of times a brand appears as that would give me duplicates. I need it unique based on if the following conditions are met:
Customer must be the same
Brand must be the same
Location must be the same
If so, we distinctly count it as one.
I know I ranted a bit and may seem to have gone in circles, but I was trying to figure out how to explain it. Please let me know if I need to edit this post or post clarification.
Many thanks in advance as I go through my journey with DAX!
I believe I have the answer. I used a NATURALINNERJOIN in DAX to create a new, merged table since I needed to reference all values in the same query (couldn't figure out how to do it otherwise). I also created an "unique identity" calculated column that combined data from multiple rows, but was hidden behind the scenes (not actually displayed on the report) so I could then take a measure of the unique values that way.
TotalDisplays = COUNTROWS(DISTINCT('GD-DP-Merge'[DisplayCountCalcCol]))
My calculated column is as follows:
DisplayCountCalcCol = 'GD-DP-Merge'[CustID] & 'GD-DP-Merge'[Brand] & 'GD-DP-Merge'[Location] & 'GD-DP-Merge'[Order#]
So the measure TotalDisplays now reports back the distinct count of rows based on the unique value of the customer ID, the brand, and the location of the item. I also threw in an order number just in case.
Thanks!
I am semi new to DAX and was struggling with Count and CountA formula, you post has helped me with answers. I would like to add the solution which i got for my query: Wanted count for Right Time start Achieved hence if anyone is looking for this kind of answer use below, filter will be selecting the table and adding string which you want to
RTSA:=calculate(COUNTA([RTS]),VEO_Daily_Services[RTS]="RTSA")

How to write my query in Relational Algebra?

I have a dataset which has files of hotel reviews. Each file contains multiple reviews for a single hotel. Here are my two relations in BCNF:
Hotel(hotelID, OverallRating, AveragePrice, URL)
Review(hotelID, Author, Content, Date, No. Reader, No. Helpful,
Overall, Value, Rooms, Location, Cleanliness, Checkin / front desk,
Service, Business Service)
I am trying to write the following query in relational algebra:
Find all the reviews by the same user (i.e., given a user ID, return the list of all their
reviews).
By User ID, the question is referring to the Author attribute found in my second relation. The way I understand the question, it must take a user ID as an argument. Maybe you see it differently?
Here is what I have so far:
(Selection) Author = $1 (Review)
Replace selection with the sigma symbol used to represent selection in relational algebra, I was having trouble inserting it into my question. $1 represents where it would take the user ID argument, this is just to show my thinking, I do not think its correct.
Thanks for your time
Query will be:
σ(Author="Your_User Id") ( Hotel Join(X)(Hotel.hotelID=Review.hotelID) Review )
Where
σ = Selection Operator
X= Join Operator
(-----) = Condition
Hope it helps. For More detail Refer My notes for DBMS: Relational Algebra
Search "Relational Algebra" Term in site to find your exact information fast.

SSRS Sorting values alphabetically within multiple columns

this is my first ever post so please be gentle with me..
I'm a novice SSRS report builder who is trying to achieve something I beleive is so simple, but I can't seem to fathom it. Any assistance or general points in any direction would be appreciated.
My data looks like this:
ID Measure 1 Barrier 1 Measure 2 Barrier 2 Measure 3 Barrier 3
01 Replace Lights Finance Review Contract Time Solar Panels Finance
02 Review Contract Time Solar Panels Time Replace Boiler Finance
03 Replace Boiler Disruption Replace lights Disruption Solar Panels Disruption
I need to be able to count how many times Finance is a barrier to each measure and how many times Disruption is a barrier to each measure and so on...
I think I should group or sort the data in order to apply a count expression but I am not sure of the best way to group or sort the data effectively to be able to render the results in a report.
Thanks in advance
Frustrated novice (aka JMS)
You should give a lot more information, I dont know what version of SQL you are using nor do I know where you get your data (if it is from sql it would be possible to get this data directly in the table from sql.)
In your dataset, add a expression column like this :
=SUM(IIF(Fields!Barrier1.Value = "Finance",1, 0))
and name it "Barrier1count"
in your table, right click on the last row, select insert row, outside group, bellow.
under any measure column, do a right click select expression, and paste this
=SUM(Fields!Barrier!Value)

Resources