Countif in DAX PowerPivot - dax

I need to calculate the average weekly quantity of a month in Power Pivot, so I need to count how many weeks in the column of "Wkly #" on the 1st table "Fcst Volume" from the 2nd table "Period". For example, I need it to be 5 for the 1st "Wkly #" as there are 5 "P03" on the 2nd table and I need it to be 4 for the 2nd "Wkly #" as there are 4 "P04" on the 2nd table.
Can someone please tell me how to do Countif in DAX? Many thanks

When you do countif/sumif, it is an aggregation by partition. The direct dax equivalent is ALLEXCEPT
If you have table like following
You can write the calculated column like following
=CALCULATE(COUNT(Table1[Column1]),ALLEXCEPT(Table1,Table1[Column1]))

Create a new column in Table1:
=CALCULATE(COUNTROWS('Table2'),FILTER(Table2,'Table2'[Period]='Table1'[Period]))

Related

Power BI match one field in another table, then return a different field from same record and filter multiple results

Task: calculated Table A contains 2 columns from Table B, and a filtered column from Table C that is based on values from a column in Table A.
Table A
Work Pack Job Job Start Date
123 00A (value from column "Start Date" in Table C, matches Job)
Problem: I cannot figure out DAX to populate [Job start date].
DAX to populate Work Pack and Job
Table A = SUMMARIZE (
'work history'[work pack]
'work history'[job]
)
I have tried variations around FILTER but cannot deduce how to provide [job] as a key, then filter on the multiple values for [Job start date]. Some jobs have many start dates and I want only the first.
If this was SQL it would be
SELECT * From TableC where [job no] = TableA.[job no]
LIMIT 1
ORDER BY [Job start date]
tried linking Table A to table C on Job, then using RELATED, but error indicates the required columns do not have a relationship. It's ugly because it is a "many to many" join.
there are lots of samples for SUMMARIZE and FILTER that seem pretty close to what I need, but don't quite close the gap for using a key to query and filter a third table.
DAX is a new thing for me and currently it's a struggle to do stuff that would be a 10 minute job in Excel or Access.
Edit: this site is pretty close to what I had in mind but the solution is to generate a table, not a column:
https://www.sqlservercentral.com/articles/dax-for-sql-folks-part-ii-translating-sql-queries-to-dax-queries
Try this:
Table A = SUMMARIZE ('work history'
'work history'[work pack],
'work history'[job],
"TableCCol",
max('TableC'[Column])
)

PowerBI - Lookup by Multiple Criteria

I'm a new PBI user and would like help on the following:
I have 2 tables (Table 1 & 2). Table 1 is a bookings report showing sales orders, part numbers and order value. Table 2 is a margin report showing sales orders, part numbers with additional descriptive text and margin value.
I would like to copy margin values from Table 2 into a new column in Table 1 by looking up by sales order and part number.
Any help would be appreciated!
Tables1
Extract text using the LEFT function
Use the COMBINEVALUES function to create new column in each table. The new column will merge two existing old columns.
Depending if you have unique values in the lookup table, use LOOKUPVALUE function, if not, use this approach: DAX lookup first non blank value in unrelated table

Sorting results based on current year sales in multi-year table and creating rank column SSRS

I am newish to SSRS and I am trying to create a report where where each row displays sales for the selected year and the previous 2 years in separate columns. I would like to sort the results by the current year's sales and create a rank column and create a parameter allowing the user to choose the top N rows they wish to display. I see the tablix sorting tab which allows you to select an expression to sort by, but how would I specify that I want to sort by the sales for one year specifically? Or would I go about this by creating the rank column by that expression first then sorting the table by that rank column?
UPDATE: It is a SQL Server database and I can edit the SQL. For the columns I have storeID, storeName, Year, and Sales.

Custom column to sum values from second table with conditional statement

I have two tables, the first table has a list of invoice numbers and the second table has a list of products associated with each invoice. I want to sum the total cost of the products for each invoice and include it in the first table using Excel Power Query.
Table 1
[InvoiceNumbers] [OtherData]
Table 2
[Product] [Amount] [InvoiceNumber]
List.Sum() seems to be the function I need to use, but I cannot filter table 2 by invoice number using this function
Table.SelectRows() can be used to select the second table, and filter it to a specific set of rows, but I cannot seem to filter the rows of Table 2 using a column from Table 1.
I have also looked into Grouping and joining the table, but because of other factors I have left out, this is not going to work.
The full query Im working with looks like this.
List.Sum(Table.SelectRows(Table2, each [InvoiceNumber] = [InvoiceNumber])[Amount])
This just returns the sum of all the rows because [InvoiceNumber] is equal to itself.
How can I reference the Invoice Number of the row in Table 1 to use it as a condition in the Table.SelectRows() function? Or is there a better way to get the data sum the from Table 2?
Table 1 Final
[InvoiceNumber] [OtherData] [SumOfAmounts]
If there is a restriction to group the invoice details table, you could just reference it, and group the reference
1) Reference the table:
2) Group the referenced table:
3) Then merge the reference table and expand the total column
If this helps please remember to mark the answer

Paritioning and Bucketing in Hive

My hive table will have call record data.
3 columns of the table are field1- CALL_DATE, field2-FROM_PHONE_NUM, field3- TO_PHONE
I would query something like
1) i want to get all call records between particular dates.
2) I want to get all call records for a FROM_PHONE phone number between certain dates.
2) I want to get all call records for a TO_PHONE phone number between certain dates.
My table size is approximately 6TB.
Can i know How do i need to apply partitioning or bucketing for better performance of all of my queries?
Your requirement is always to get data between certain dates and do filtering on it, so do table partition biased on date .
How to create Link for dynamic partition
You can have partition key date as yyyymmdd .
(like -- 20170406 for today(6th april 2017 ))

Resources