Left join in tabular model - left-join

I have 2 tables as below.
BILLING
AccountId RatingDate Load
1280582 2018-01-04 15:20:13.000 130.000
421947 2018-01-04 11:44:58.000 176.000
1033717 2018-01-04 10:52:23.000 234.000
RATING
AccountId RatingDate RatingMethod
1280582 2018-01-04 15:20:13.000 A
421947 2018-01-04 11:44:58.000 A
1033717 2018-01-04 10:52:23.000 A
I need to create a measure in Billing table to calculate the sum of Load column. I have the SQL query to perform the same but I am not able to create the measure with the same logic.
SELECT SUM(b.Load)
FROM Billing b
LEFT JOIN Rating r
ON b.AccountId = r.AccountId
AND b.RatingDate = r.RatingDate
WHERE r.RatingMethod = 'A'
Can someone help me with the dax formula for creating the measure?

There are a few ways to approach this including
Do the left join in the query editor.
Create bridge tables or unique indexing columns to relate your two tables.
Write a measure using the LOOKUPVALUE function.
If you're doing a bunch of other stuff, you'll probably want to consider option #1 or #2, but for just this particular measure, I'll show you #3.
LoadSum = CALCULATE(SUM(Billing[Load]),
FILTER(Billing,
LOOKUPVALUE(Rating[RatingMethod],
Rating[AccountID], Billing[AccountID],
Rating[RatingDate], Billing[RatingDate])
= "A"))
The above sums over the [Load] after filtering rows where [RatingMethod] = "A" in the Rating table (and matching on the ID and date columns).
For #1, select your Rating query and click Merge Queries (Home tab).
Select the columns you want to join one (Ctrl+Click in the same order on both of them). Keep the default Left Outer Join and hit OK.
Once they're merged, you'll have a new column named Billing. Click the expand icon in the top right corner of the column and select which columns you want to expand (select Load only).
Once expanded, your table should look like this:
RATING
AccountId RatingDate RatingMethod Load
1280582 2018-01-04 15:20:13 A 130
421947 2018-01-04 11:44:58 A 176
1033717 2018-01-04 10:52:23 A 234

Related

In tableau how to show unmatched row in my case?

I'm new to tableau.
I have a data like this.
[department]
[employee]
And I want to get employee count under 30 age per department. like this..
[my goal]
I want to show all departments (if there is no employee)
To get result, I do these steps,
make relationship of dept - emp (one datasource)
make datasource filter (emp.age < 30)
make sheet and set row as dept_no, countd(emp_no)
But I could only get like this.
[my result]
How can I get my goal..!!??
Help me!!
It is simple hereafter. Use join (instead of relationship) on the side of dept table and Take dept_no column from dept_table instead of emp_table. To do this follow these steps
double click on first added table (say emp). A join window will open (thgis differs from relationship)
add dept table and use right join
right click age convert to dimension
right click again age and convert to continuous
create filter at most 29 on age and don't forget to check include null values in the table
If you want to add all values in view. create a calc field with calculation as
INT([Age] <30)
add sum of this field to view
edit the calculation to show all nulls as 0
ZN(INT([Age] <30))

How can I sum the same column differently depending on another column type

I have a SQL Query I want to create in Alteryx.  This query goes through an Invoicing table and sums up different charges depending on the location that is associated with it.  So each location can have different types for each column.  Below is an example:
Location FreightCharge InvoiceCharge
A FRT IVC
B FRT, CHG IVC, DTL
Using a query with a case statement worked fine when I only had one location to worry about.  Now I will potentially have hundreds.  I created a Locations table that has the types in it (the table above).  But I'm not convinced it's the right way to do this.
ultimately I would either want a SQL Statement (alteryx) that can do this from the ground up or (more likely) a workflow I can use to loop through locations which links to my reference table and automatically tallies this data.
Any ideas?
However, it would be nice if I could run this for each location, appending it to an invoice table.  
I've tried creating a reference table and joining it to my main invoicing query but I can't get it to filter by the correct data. I've considered creating a Alteryx or SSIS Package that loops through each record, however I don't need to loop through things one record at a time. Just one location at a a time.
This would go into a table like this for location A:
Select Location, InvoiceNumber, Case WHEN InvoiceType IN('FRT') THEN InvoiceAmount Else 0 End as FreightCharge --**I want this to be dynamic depending on location, Case WHEN InvoiceType IN('IVC','DTL') then InvoiceAmount Else 0 End as InvoiceCharge --**I want this to be dynamic depending on location From Invoicing Where InvoiceDate = Today
I would want an invoicing table with all locations and their summed values (how they should sum according to the reference table) all in one table.
Suppose you organize your lookup table as:
Location GroupLabel InvoiceType
A Freight FRT
A Invoice IVC
B Freight FRT
B Freight CHG
B Invoice IVC
B Invoice DTL
And it sounds like your data is something like...
Location InvoiceNumber InvoiceAmount InvoiceType
A 1 1.00 FRT
A 1 2.00 IVC
A 2 3.00 FRT
A 2 4.00 IVC
B 3 5.00 FRT
B 3 6.00 IVC
B 4 7.00 CHG
B 4 8.00 DTL
Then, I think the following would provide what you're after...
Select
i.Location
, i.InvoiceNumber
, l.GroupLabel
, Sum(i.Amount)
From
Invoicing i
Inner Join LookupTable l
on l.Location = i.Location
and l.InvoiceType = i.InvoiceType
Group By
i.Location
, i.InvoiceNumber
, l.GroupLabel
In Alteryx, it should be pretty straightforward to do the Join on both Location and InvoiceType before doing an Aggregation grouped by the three fields shown and summing InvoiceAmount.

Show the same field twice, side by side, with different values

How can I want print the same field twice with different value for each?
My Product and Shop tables are linked with product_id as the primary/foreign key.
Product
product_id
product_name
Shop
shop_id
product_id
quantity_product
When I use Expert Selection, I take all products on Shop_id 1 and Shop_id 2 (if Shop_id is in {1,2}...). Now I want to print 2 different quantity_product fields from Shop 1 and Shop 2 in the same line as my product_id. Something like:
product_name quantity_product(shop_id: 1) quantity_product(shop_id: 2)
1 10 20
I tried adding a new link in my database scheme with another Shop table, but it didn't work out.
You can set up a CrossTab in the summary section to handle any number of Shops. (So long as there's room on the report for all the shop columns)
If that doesn't work you can manually set up a summary for each Shop:
Keep in mind both of these solutions will need to be placed in a Footer section instead of the Details.

Pivot Charts in google Sheets by counting non-numeric data?

I have a dataset that I'd like to summarize in chart form. There are about 30 categories whose counts I'd like to display in a bar chart from about 300+ responses. I think a pivot table is probably the best way to do this, but when I create a pivot table and select multiple columns, each new column added gets entered as a sub-set of a previous column. My data looks something like the following
ID Country Age thingA thingB thingC thingD thingE thingF
1 US 5-9 thB thD thF
2 FI 5-9 thA thF
3 GA 5-9 thA thF
4 US 10-14 thC
5 US 10-14 thB thF
6 US 15-18
7 BR 5-9 thA
8 US 15-18 thD thF
9 FI 10-14 thA
So, I'd like to be able to create an interactive chart that showed the counts of "thing" items; I'd then like to be able to filter based upon demographic data (e.g., Country, Age). Notice that the data is non-numeric, so I have to use a CountA to see how many there are in each category.
Is there a simple way to display chart data that summarizes the counts and will allow me to filter based on different criteria?
The query can summarize the data in the form you want. The fact that you have "thA", "thB", etc, instead of "1" complicates the matter, but one can transform the strings to numeric data on the fly.
Assuming the data you've shown is in the cells A1:I10, the following formula will summarize it:
=query({B2:C10, arrayformula(if(len(D2:I10), 1, 0))}, "select Col1, Col2, count(Col3), sum(Col3), sum(Col4), sum(Col5), sum(Col6), sum(Col7) group by Col1, Col2", 0)
Explanation:
{B2:C10, arrayformula(if(len(D2:I10), 1, 0))} creates a table where the first two columns are your B,C (Country, Age) and the other six are filled with 1 or 0 depending on whether the cells in D-I are filled or not.
select Col1, Col2, count(Col3), sum(Col3), ... group by Col1, Col2 selects Country, Age, the total count of rows with this Country-Age combination, the number of rows with thingA for this Country-Age combination, etc.
the last argument, 0, indicates there are no header rows in the table passed to the query.
It's possible to give labels to the columns returned by the query, using label: see query language documentation. It would be something like
label Col1 'Country', Col2 'Age', count(Col3) 'Total count', sum(Col3) 'thingA count', ...
Add a Count column to your data with a "1" for whatever occurrence, this might solve your problem in the Pivot Table. I was just looking for a solution and thought about this. Working now for me.

MicroStrategy - Dynamic Attribute with join

In our MicroStrategy 9.3 environment, we have a star schema that has multiple date dimensions. For this example, assume we have a order_fact table has two dates, order_date and ship_date and an invoice_fact table with two dates invoice_date and actual_ship_date. We have a date dimension that has "calendar" related data. We have setup each date with an alias, per the MicroStrategy Advanced Data Warehousing guide, which is MicroStrategy's recommended approach to handling role-playing dimensions.
Now for the problem. The aliased dates allow for users to create reports specific to the date that has been aliased. However, since the dates have been aliased, MicroStrategy won't combine "dates" as they appear to it to be different. Case in point, I can't easily put on a report that shows order quantities and invoice quantities by order_date and invoice_date as it results in a cross join.
The solution we have been talking about internally, is creating a new attribute called order_fact_date and an invoice_fact_date. These dates would be determined at runtime via the psuedo code below:
case when <user picked date> = 'order date'
then order_date
else ship_date end as order_fact_date
case when <user picked date> = 'invoice date'
then invoice_date
else actual_ship_date as invoice_fact_date
Our thinking was then, we could have a "general" date dimension mapped to both dates which would enable MicroStrategy to leverage the same table in the joins and thereby eliminating the cross join issue.
Clear as mud?
Edit 1: Changed "three dates" to "two dates".
if I have understood correctly your problem, you have created multiple dates attributes (with different logical meaning) and they are mapped on different aliases of the calendar table.
Until users use different a single fact table in their reports there is no problem, but when they use metrics/facts from sales and invoices you have multiplied results because "Order Date" and "Invoice Date" are different attributes.
Your SQL looks something like:
...
FROM order_fact a11
INNER JOIN invoice_fact a12
INNER JOIN lu_calendar a13
ON a11.order_date = a13.date_id
INNER JOIN lu_calendar a14
ON a12.invoice_date = a14.date_id
...
As usual there are possible solution, not all of them very straight forward.
Option 1 - Single date attribute
You mention this possibility in your question, instead of using "Order Date" and "Invoice Date", just use a single "Date" attribute and teach users to use it. You can call it "Reporting Date" or "Operation Date" if this makes the life easier for them.
The SQL you should get is something like:
...
FROM order_fact a11
INNER JOIN invoice_fact a12
ON a11.order_date = a12.invoice_date
INNER JOIN lu_calendar a13 -- Only one join
ON a11.order_date = a13.date_id -- because the date is the same
...
Option 2 - We need to keep the two date attributes!
Map "Order Date" and "Invoice Date" on the same alias of your calendar table. This is usually can cause problems in MicroStrategy, because two attributes will be joined together on the same look-up table [see later on this], but in your case this is exactly what you are looking for.
With this solution you should get an SQL like this:
...
FROM order_fact a11
INNER JOIN invoice_fact a12 -- Hey! this is again a cross join!
INNER JOIN lu_calendar a13
ON a11.order_date = a13.date_id -- Relax man, we got you covered.
AND a12.invoice_date = a13.date_id -- Yes, we do it!
...
This is nice, but it works only if you have description forms coming from the calendar table (this is not always the case with dates because the ID is usually also the actual value that you show on your reports). In case you don't have a join with the calendar lookup, you SQL will end up again with duplicated result:
...
FROM order_fact a11 -- Notice no join column between the two facts
INNER JOIN invoice_fact a12 -- and no other conditions will help to join them
...
For this reason if you want to keep the two attributes separate, beside mapping them on the same lookup, you should also:
Create an hidden attribute (let's call it "Date_on_fact") map it on the fact table and the calendar table and make it child of both "Order Date" and "Invoice Date".
Un-map the "Order Date" and "Invoice Date" from the fact tables.
The idea here is to force MicroStrategy to use always the SQL code always the calendar lookup table:
...
FROM order_fact a11
INNER JOIN invoice_fact a12 -- This is like the previous one
INNER JOIN lu_calendar a13 -- But I'm back to help you
ON a11.order_date = a13.date_id
AND a12.invoice_date = a13.date_id
...
The attribute "Date_on_fact" can actually be hidden and users don't need to put it in their reports, but MicroStrategy will use it to go from the parent attributes to the fact table.
Hope this can help you to get out from the mud.
We had a same problem.
We had to create a generic time hierarchy for this and connected 2 different invoice and order time hierarchies to the generic one.
It works like charm!