DAX - get category by matching column from two tables - powerpivot

I have a table with column 'Product' and Lookup table with 'Product Description' and 'Category' columns as follows:
TableMain
('Product')
"Bike"
"Sport-100 Helmet"
Lookup
('Product Description','Category')
"4-Bike", "Bike"
"Sport-100 Helmet, Black", "Helmets"
I need a calculated column 'Category' in TableMain by matching TableMain.'Product' and Lookup.'Product Description' and getting the Category. as follows:
TableMain
('Product', 'Category')
"Bike", "Bike"
"Sport-100 Helmet", "Helmets"
Please suggest a DAX.
Thanks in advance.

It has been solved using CHARINDEX in SQL.
Thanks

Related

select all "groups/partitions" where parameter is found

Without going into too much detail - I need to create groups (grouped on a specific field) of data and then display all GROUPS of records that contain a parameter. I need all records in a GROUP even if some do not match the parameter. Any GROUPS where no records contain the parameter would be suppressed.
I'm working with db2 and I just need help with the basic syntax. I'm thinking a PARTITION_BY used within a subquery might be the correct approach. Any ideas? Thanks in advance.
Does it answer the question ?
with table1 (group_column, expression, other_column) as (
values
('group1', 'false', 'First in G1'),
('group1', 'false', 'Second in G1'),
('group2', 'false', 'First in G2'),
('group2', 'true', 'Second in G2'),
('group3', 'true', 'Full G3')
)
select
table1.group_column, expression, other_column
from table1
inner join
(
select
distinct group_column
from table1
where expression = 'true'
) as groups on table1.group_column = groups.group_column
GROUP_COLUMN
EXPRESSION
OTHER_COLUMN
group2
false
First in G2
group2
true
Second in G2
group3
true
Full G3

Link records to others

I have a table with commissions for example: https://i.stack.imgur.com/WWgTe.png
And second table with relations between them: https://i.stack.imgur.com/f3472.png
How to link all commissions which commission_number ends with "/1" to other with the same number?
Example:
222/2021/1 (parent) link to 222/2021/2, 222/2021/3, 222/2021/4... (child).
Can I link all commissions to his parents with one procedure?
You could construct the parent number by replacing the suffix with "1" and aggregate. The following gives a list of all related commission numbers:
select regexp_replace(commission_number, '[0-9]+$', '1') as parent_commission_number,
array_agg(commission_number)
from t
group by parent_commission_number;
You can do it like that
insert into commission_to_commission(child_commission_id,parent_commission_id)
select c_child.commission_id,c_parent.commission_id
from commission c_child, commission c_parent
where c_parent.commission_number = left(c_child.commission_number,9) || '1'
and right(c_child.commission_number,2) <> '/1';
See example here Fiddle

SQL Server query stuck when adding order by clause

I'm working on writing some queries for a very large (and messy) database on behalf of a client. So far, I've just been grabbing the columns I need and making sure that they contain the data I'm looking for. There have been no issues up until I add anything to my "order by" clause. When I add an order by clause to my query, the SQL Server remains stuck on "executing query." Example of code below:
SELECT TOP 100
PEATR.[EffectiveDate] AS 'Effective Date',
CustomerRoot.[CustomerName] AS 'Name',
CustomerAddressRoot.[Street1] AS 'Address 1',
CustomerAddressRoot.[Street2] AS 'Address 2',
CustomerAddressRoot.[City],
CustomerAddressRoot.[State],
CustomerAddressRoot.[Zip],
CustomerAddressRoot.[Country],
CustomerAddressRoot.[AddressDesc] AS 'Description'
FROM PrEmployeeAccrueTierRoot AS PEATR, CustomerRoot, CustomerAddressRoot
ORDER BY CustomerRoot.[CustomerName]
I have also tried creating an inner join on CustomerRoot and CustomerAddressRoot, as the query is returning repeat data, especially in the "Address 1" column. When I ran the code below, I have received the following error message:
The objects "CustomerRoot" and "CustomerRoot" in the FROM clause have the same exposed names. Use correlation names to distinguish them.
Code:
SELECT TOP 100
PEATR.[EffectiveDate] AS 'Effective Date',
CustomerRoot.[CustomerName] AS 'Name',
CustomerAddressRoot.[Street1] AS 'Address 1',
CustomerAddressRoot.[Street2] AS 'Address 2',
CustomerAddressRoot.[City],
CustomerAddressRoot.[State],
CustomerAddressRoot.[Zip],
CustomerAddressRoot.[Country],
CustomerAddressRoot.[AddressDesc] AS 'Description'
FROM PrEmployeeAccrueTierRoot AS PEATR, CustomerRoot, CustomerAddressRoot
INNER JOIN CustomerRoot ON CustomerAddressRoot.CustomerId=CustomerRoot.CustomerId
I did assign aliases to all of the tables previously, though I was still returning the same error message. Any guidance or suggestions would be much appreciated.
You are performing a cartesian join.
FROM PrEmployeeAccrueTierRoot AS PEATR, CustomerRoot, CustomerAddressRoot
...is the same as...
FROM PrEmployeeAccrueTierRoot AS PEATR
inner join CustomerRoot on 1=1
inner join CustomerAddressRoot on 1=1
There is no join logic.
So, if PrEmployeeAccrueTierRoot has 1000 rows and CustomerRoot has 1000 rows and CustomerAddressRoot has 1000 rows, your result will have 1,000,000,000 rows.
Try two things:
Include join logic.
Your query should look something like this:
SELECT TOP 100
PEATR.[EffectiveDate] AS 'Effective Date',
CustomerRoot.[CustomerName] AS 'Name',
CustomerAddressRoot.[Street1] AS 'Address 1',
CustomerAddressRoot.[Street2] AS 'Address 2',
CustomerAddressRoot.[City],
CustomerAddressRoot.[State],
CustomerAddressRoot.[Zip],
CustomerAddressRoot.[Country],
CustomerAddressRoot.[AddressDesc] AS 'Description'
FROM PrEmployeeAccrueTierRoot AS PEATR
inner join CustomerRoot on CustomerRoot.customerrootid = peatr.customerrootid
inner join CustomerAddressRoot on CustomerAddressRoot.customerrootid = CustomerRoot.customerrootid
ORDER BY CustomerRoot.[CustomerName]
Of course, I don't know what columns you should actually join on. Know thy data.
Then you'll have one problem remaining: If your query (without the TOP 100) would return millions of rows, you're asking the database server to perform all of the logic and gather all of the rows, then sort them by CustomerName, then return the first 100 rows. That could still be slow. You'll want to...
Apply filters.
SELECT TOP 100
PEATR.[EffectiveDate] AS 'Effective Date',
CustomerRoot.[CustomerName] AS 'Name',
CustomerAddressRoot.[Street1] AS 'Address 1',
CustomerAddressRoot.[Street2] AS 'Address 2',
CustomerAddressRoot.[City],
CustomerAddressRoot.[State],
CustomerAddressRoot.[Zip],
CustomerAddressRoot.[Country],
CustomerAddressRoot.[AddressDesc] AS 'Description'
FROM PrEmployeeAccrueTierRoot AS PEATR
inner join CustomerRoot on CustomerRoot.customerrootid = peatr.customerrootid
inner join CustomerAddressRoot on CustomerAddressRoot.customerrootid = CustomerRoot.customerrootid
WHERE CustomerAddressRoot.State = 'Alaska'
ORDER BY CustomerRoot.[CustomerName]
..at least during testing, to speed things up.

SAP query. Retrieve values basing on input on selection screen

I am stuck with coding a SAP query..I am new to ABAP.
What I would like to achieve is a join between tables ESLL, EKPO, EKKO.
Specifically these are the steps I would like to achieve:
in the selection parameter every time I will enter the query I will
give a different value for ESLL-EXTSRVNO;
basing on that value the query automatically should select ESLL-PACKNO basing on ESLL-EXTSRVNO given;
then the query should put ESLL-SUB_PACKNO equal
to the ESLL-PACKNO values of the steps before;
then the query should
put the new ESLL-PACKNO values equal to EKPO-PACKNO and retrieve the
following fields: EKPO-EBELN, EKPO-EBELP, EKPO-MATKL.
I have already written some code inside the infoset, but I do not know how to fix it.
In the "data" section I have written:
DATA: it_esll TYPE TABLE OF esll.
DATA: it_esll2 TYPE TABLE OF esll.
DATA: it_ekpo TYPE TABLE OF ekpo.
In the "start-of-selection" section I have written:
SELECT packno
FROM esll
INTO TABLE it_esll.
IF sy-subrc EQ 0.
SELECT packno FROM esll
into TABLE it_esll2
for ALL ENTRIES IN it_esll
where sub_packno EQ it_esll-packno.
IF sy-subrc EQ 0.
SELECT ebeln ebelp bukrs werks matkl menge netpr peinh
FROM ekpo
into TABLE it_ekpo
for ALL ENTRIES IN it_esll2
WHERE packno EQ it_esll2-packno.
endif.
endif.
And, in order to display all the information I want, I have put the following joins:
ESLL-PACKNO --> EKPO-PACKNO --> EKPO-EBELN --> EKKO-EBELN
At then end I would like to display these information:
EKPO-EBELN
EKPO-EBELP
EKPO-MATKL
EKKO-BSART
EKPO-PACKNO
Could you please help me?
One option could be to use Alias table in your infoset, something like this:
First table: ESLL;
Second table ZESLL (Alias on ESLL) with join ZESLL-PACKNO = ESLL-SUB_PACKNO;
Third table: EKPO with join on EKPO-PACKNO = ZESLL-PACKNO;
Fourth table: EKKO with join on EBELN;
So you can avoid ABAP
Infoset Join

Translate Value to Name SQL

I have 2 tables
Instead of Currency1Id and Currency2Id, I want to show the CurrencyShortcut just to make it look better
In any case I managed to join only one of the fields like this :
Please help me convert both of them to the values for the currency table
Thanks !!
I think what you need is something like this
SELECT c1.CurrencyShortcut [Currency1], c2.CurrencyShortcut [Currency2]
/*, other columns you need*/
FROM Spot as s
JOIN Currencies as c1 ON s.CurrencyId = c1.CurrencyId
JOIN Currencies as c2 ON s.CurrencyId = c2.CurrencyId
You can alias your columns too..
SELECT
c1.Currency1Id AS Currency1Shortcut
, c2.Currency2Id AS Currency2Shortcut