Power Query M formula language foreign key checking - powerbi

I am currently using the Power BI Power Query Editor writing queries using the Power Query M formula language. I currently have two tables named employee and business. Each row of the employee table has a business_id that connects to a row of the business table. I want to run a Table.SelectRows function to include only the rows where employee.business_id = business.id.
This is what I have so far:
let
Source = MySQL.Database(<DATABASE>, "business_database", [ReturnSingleDatabase=true, CreateNavigationProperties=false]),
business_database_employee_all = Source{[Schema="business_database",Item="employee"]}[Data],
employee_included = . . . Return all rows from employee where employee.business_id = business.id . . .
in
employee_included
Any sort of help with this one would be appreciated! I'm pretty set on using Table.SelectRows but I'm down to utilize better functions if it's recommended!

It sounds like you want to filter the employee table so that it only shows rows where there is a match in the business_id field to the business table
the best way is to merge the business table into the employee table, matching the business_id field, with inner join
#"Merged Queries" = Table.NestedJoin(#"PriorStepNameinEmployeeTable",{"business_id"},business,{"business_id"},"business",JoinKind.Inner)
another way
#"Select" = Table.SelectRows(#"PriorStepNameinEmployeeTable", each Table.Contains(business_table,_,{"business_id"}))

Related

Power BI LOOKUPVALUE with a column of values for the search items? (VLOOKUP alternative)

In Power BI, I need to create a VLOOKUP alternative. From the research I've done, this is done with the LOOKUPVALUE function, but the problem is that function needs one specific SEARCH ITEM, which isn't super helpful in a VLOOKUP type scenario where you have a full column of values to search for?
Given these two tables, connected through the user_name and first_name columns:
...what's the formula needed in order to create a new column in the Employee_Table called phone_call_group by using the names as the search items in order to return the group they belong to? So how can I end up with this?
(Forget that the entries in each table are already sorted, needs to be dynamic). Will be back tomorrow to review solutions.
In Power BI you have relations between tables instead of Excel's VLOOKUP function.
In your case you just have to create a one-to-one relation between
'Phone_Call_Table'[user_name] and 'Employee_Table'['first_name]'
With that you can add a Calculated Column to your 'Employee_Table' using the following expression:
phone_call_group = RELATED(Phone_Call_Table[group])
and in the data view the table will look like this:
LOOKUPVALUE() is just a workaround if for other reasons you can't establish that relation. What you've been missing so far is that in a Calculated Column there is a Row Context which gives you exactly one value per row for the <search_value> (this is different from Measures):
alt_phone_call_group =
LOOKUPVALUE(
Phone_Call_Table[group],
Phone_Call_Table[user_name],
Employee_Table[first_name]
)

Netsuite suiteql how to get all available tables to query?

I am using Postman and Netsuite's SuiteQL to query some tables. I would like to write two queries. One is to return all items (fulfillment items) for a given sales order. Two is to return all sales orders that contain a given item. I am not sure what tables to use.
The sales order I can return from something like this.
"q": "SELECT * FROM transaction WHERE Type = 'SalesOrd' and id = '12345'"
The item I can get from this.
"q": "SELECT * FROM item WHERE id = 1122"
I can join transactions and transactionline for the sale order, but no items.
"q": "SELECT * from transactionline tl join transaction t on tl.transaction = t.id where t.id in ('12345')"
The best reference I have found is the Analytics Browser, https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2021_1/analytics/record/transaction.html, but it does not show relationships like an ERD diagram.
What tables do I need to join to say, given this item id 1122, return me all sales orders (transactions) that have this item?
You are looking for TransactionLine.item. That will allow you to query transaction lines whose item is whatever internal id you specify.
{
"q": "SELECT Transaction.ID FROM Transaction INNER JOIN TransactionLine ON TransactionLine.Transaction = Transaction.ID WHERE type = 'SalesOrd' AND TransactionLine.item = 1122"
}
If you are serious about getting all available tables to query take a look at the metadata catalog. It's not technically meant to be used for learning SuiteQL (supposed to make the normal API Calls easier to navigate), but I've found the catalog endpoints are the same as the SuiteQL tables for the most part.
https://{{YOUR_ACCOUNT_ID}}.suitetalk.api.netsuite.com/services/rest/record/v1/metadata-catalog/
Headers:
Accept application/schema+json
You can review all the available records, fields and joins in the Record Catalog page (Customization > Record Catalog).

Power BI Dax Create New Table From Existing Columns

Newbie with DAX, trying to do something which I thought would be very simple but already hitting a bit of a wall and would appreciate any assistance.
I have table 'General' with columns:
'name', 'age', 'dob', 'address', 'home number', 'state'.
I have table 'Work' with columns:
'name','work address', 'company', 'mobile', 'email'.
I want to create a new (virtual?) table made of the following columns:
'General'[name], 'General'[age], 'General'[state], 'Work'[company], 'Work'[email]
Googling around, some of the solutions for creating a new table look really messy.. isn't there a super simple way to construct this? Thanks!
There are two option, first your can use "Merge Queries" option in the "Power Query Editor". You can merge two or more table this way based on Key column available in both tables. You can also use more than one Key columns for joining purpose. You can add new columns to your left table Or, you can create a new table from those two tables which is my preference. For more details, you can check this below link-
https://radacad.com/how-to-change-joining-types-in-power-bi-and-power-query#:~:text=In%20Power%20BI%20Desktop%20you,Right%20part%20of%20the%20join).
Option 2 is to create two custom columns 'Work'[company] & 'Work'[email] in the table "General". To generate value in those columns, you can use LOOKUPVALUE. Here is a sample code for that -
company =
LOOKUPVALUE(
'Work'[company]
'Work'[id], 'General'[id]
-- Guess you have common ID column in both table for joining
)

Obtain MAX of column and display for each row

I'm trying to obtain the MAX of a particular column in a Power BI Report and place this as a new Measure within each ROW of the same dataset. Please see the example below.
Is this possible in DAX and via DirectQuery/LiveConnection? The report is pointing to a tabular model but due to outside factors the measure must be created in the report.
Thanks
You can accomplish this a few ways. Essentially, you need override the filter context so that the MAX function isn't just running over whatever slice you're showing in the visual. Using CALCULATE or the iterator function MAXX, set the wrap the table in the ALL() function to override the context and calculate max over all rows.
= CALCULATE(MAX([Calendar`Year]), ALL('Smithfield_Fiscal_Calendar'))
or
= MAXX(ALL('Smithfield_Fiscal_Calendar'), [Calendar`Year])
To get the breakout by date, you'll need to include a Date table in your model. PowerBI makes this possible with a few different DAX options. As an example, go to your Model tab, click 'New Table' and put in the following expression:
MyCalendar = CALENDAR(DATE(2019,1,1), DATE (2019,1,10))
This is a little trivial -- you'd want to use a useful range of dates but this one matches your example above. Next, add a column to [MyCalendar]
CalendarMonthYear = month([date]) & "-" & year([date])
Go to your budget table and add a similar field
BudgetMonthYear = month([date]) & "-" & year([date])
Go into your Model view and create a relationship between CalendarMonthYear and BudgetMonthYear. This will associate every date in the date table with the particular budget row from your budget table.
Hope it helps.

PowerBI DAX function to count number of occurrences using DISTINCT (Countif)

I am trying to create a column in PowerBI that counts how times a customer name occurs in a list.
I may be going in the wrong direction, but what I have so far is;
My SQL query returns a table of customer site visits (Query1), from which I have created a new table (Unique) and column (Customer) that lists the distinct names (cust_company_descr) from Query1;
Unique = DISTINCT(Query1[cust_company_descr])
What I need is a new column in the Unique table (Count) that will give me a count of how many times each customer name in the Query1 table appears.
If I were working in Excel, the solution would be to generate a list of unique values and do a COUNTIF, but I can't find a way to replicate this.
I have seen a few solutions that involve this sort of thing;
CountValues =
CALCULATE ( COUNTROWS ( TableName ); TableName[ColumnName] = " This Value " )
The issue I have with this is the Unique table contains over 300 unique entries, so I can't make this work.
Example (The Count column is what I'm trying to create)
Query 1
cust_company_descr
Company A
Company B
Company A
Company C
Company B
Company A
Unique
Company_____Count
Company A_____3
Company B_____2
Company C_____1
Any help is gratefully received.