Power Query / Power BI increment data at each occurrences with specific ranges - indexing

I am trying to combine worksheets from two different workbooks with Power Query and I have trouble doing that.
I do not would like to increment the "ID" column from one workbook given the occurrences of values in another.
The ID column has rows with letter tags : AB or BE. Following these letters, specific numeric ranges are associated. For both AB and BE, number ranges first from 0000 to 3000 and from 3000 to 6000 and then 6001 to 8000 and 8001 to 9999.
I thus have the following possibilities:
From AB0000 to AB3000
From AB3001 to AB6000
From AB6001 to AB8000
From AB8001 to AB9999
From BE0000 to BE3000
From BE3001 to AB6000
From BE6001 to BE8000
From BE8001 to AB9999
Each category match to the a specific item in my column geography, from the other workbook:
From AB0000 to AB3000, it is ItalyZ ABC
From AB3001 to AB6000 it is ItalyB BDE
From AB6001 to AB8000 it is ItalyZ ABC
From AB8001 to AB9999 it is ItalyB BDE
From BE0000 to BE3000 it is UKM ABC
From BE3001 to AB6000 it is UKY BDE
From BE6001 to BE8000 it is UKM ABC
From BE8001 to AB9999 it is UKY BDE
I am thus trying to find the highest number associated to the first AB category, the second AB category, the first BE category, and the second.
I then want to "bring" this number in the other query and increment it each time that matching country is found in the other workbook. For example :
AB356 is the highest number in the first workbook.
Once the first "ItalyB" is found, the column besides writes "AB357". Once the second is "ItalyB" is found, the column besides write "AB358".
Lets say that in the first file that I am getting I have the following data:
|---------------------|
| Test |
|---------------------|
| AB0000 |
|---------------------|
| AB0001 |
|---------------------|
| AB3034 |
|---------------------|
| AB3035 |
|---------------------|
| AB6005 |
|---------------------|
| AB6006 |
|---------------------|
| AB8011 |
|---------------------|
| AB8012 |
|---------------------|
| BE0000 |
|---------------------|
| BE0001 |
|---------------------|
| BE3091 |
|---------------------|
| BE3092 |
|---------------------|
| BE6001 |
|---------------------|
| BE6002 |
|---------------------|
| BE6003 |
|---------------------|
| BE8104 |
|---------------------|
| BE8105 |
|---------------------|
And a second file as follows:
|---------------------|
| ItalyZ ABC |
|---------------------|
| ItalyZ ABC |
|---------------------|
| ItalyB BDE |
|---------------------|
| ItalyZ ABC |
|---------------------|
| ItalyB BDE |
|---------------------|
| UKM ABC |
|---------------------|
| UKY BDE |
|---------------------|
| UKM ABC |
|---------------------|
| UKY BDE |
|---------------------|
| UKY BDE |
|---------------------|
| UKY BDE |
|---------------------|
| UKY BDE |
|---------------------|
Then my result would be as follows:
|---------------------|
| Test |
|---------------------|
| AB0002 |
|---------------------|
| AB0003 |
|---------------------|
| AB3036 |
|---------------------|
| AB6007 |
|---------------------|
| AB8013 |
|---------------------|
| BE0002 |
|---------------------|
| BE3091 |
|---------------------|
| BE3093 |
|---------------------|
| BE6004 |
|---------------------|
| BE8105 |
|---------------------|
| BE8106 |
|---------------------|
| BE8107 |
|---------------------|
| BE8108 |
|---------------------|
I have tried to adapt a query which was more limited in its scope, by adding the category, I thus have a query as follows:
let
Source = #table({"Prefix", "Seq_Start", "Seq_End","Zone"},
{{"AB",0,2999,"UKM BDE"}, {"AB",3000,4999,"UKM ABC"}, {"AB",5000,7999,"UKY BDE"}, {"AB",8000,9999,"UKY ABC"},{"BE",0,2999,"ITALYZ BDE"},{"BE",3000,4999,"ITALYZ ABC"}, {"BE",5000,7999,"ITALYB BDE"}, {"BE",8000,9999,"ITALYB ABC"}}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Seq_Start", Int64.Type}, {"Seq_End", Int64.Type}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Prefix"}, #"Source data with occurence", {"Unique key Prefix"}, "Source data with occurence", JoinKind.LeftOuter),
#"Expanded Source data with occurence " = Table.ExpandTableColumn(#"Merged Queries", "Source data with occurence", {"Number"}, {"Number"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Source data with occurence ", each [Number] >= [Seq_Start] and [Number] <= [Seq_End]),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Prefix", "Seq_Start", "Seq_End", "Zone"}, {{"NextSeq", each List.Max([Number]) + 1, type number}})
in
#"Grouped Rows"

your query says "Table.Group"
Did you try to COMBINE those two tables? i prefer "Combine and create new one".
Table-1 and Table-2' s headers were different, First i equalized them. Showed in the in the picture. then combined and created new table. Here is code section;
let
Kaynak = Table.Combine({#"Puantaj CkrEng", #"Puantaj Genel"}),
#"Filtrelenen Satırlar" = Table.SelectRows(Kaynak, each ([Firma] <> null)),
#"Yeniden Sıralanan Sütunlar" = Table.ReorderColumns(#"Filtrelenen Satırlar",{"Firma", "Resource ID", "Görev", "Tarih", "Yevmiye", "HaftaBasi"})
in
#"Yeniden Sıralanan Sütunlar"

Related

SQL to transpose and create rows based on column values

I have data in Postgres SQL, something like this
|KEY |Mobile1|Mobile2| M1_St_Date| M2_St_Date|M1_Exp_Date|M2_Exp_Date|
|----|-------|-------|-----------|-----------|-----------|-----------|
|1234|1234567|2234567|2014-01-01 |2015-01-01 |2019-01-01 |2020-01-01 |
|1235|1234568| |2013-01-01 | |2018-01-01 | |
|1236| |2234568| |2018-01-01 | |2023-01-01 |
I am trying to create something like below
| KEY | Type | Mobile Number|St Date |Exp Date |
| ------| --------| ----------- |-------- |----------|
| 1234 | Personal| 1234567 |2014-01-01|2019-01-01|
| 1234 | Office | 2234567 |2015-01-01|2020-01-01|
| 1235 | Personal| 1234568 |2013-01-01|2018-01-01|
| 1236 | Office | 2234568 |2018-01-01|2023-01-01|
Mobile 1 should be named as Personal & Mobile2 should be named as Office under Type Column
Appreciate any help!
SELECT
t.key,
phonetype,
phonenumber,
startdate,
enddate
FROM phone t
JOIN LATERAL (
VALUES
(
'Personal',t.Mobile1,t.M1_St_Date,t.M1_Exp_Date
),
(
'Office',t.Mobile2,t.M2_St_Date,t.M2_Exp_Date
)
) s(phonetype, phonenumber,startdate,enddate) ON TRUE
db fiddle

sum according to field condition

can you please help me with this on crystal reports.
|field1 | field2 |field3 |
|-----------|-----------|-------|
|code1 | abc | 12.00 |
|code2 | xyz | 11:00 |
|code3 | cde | 12.00 |
|code4 | yabc | 2.00 |
|code5 | xabc | 2.00 |
|code6 | xxyzx | 3.00 |
|code7 | fgfgf | 43.00 |
code8 and so on....
i want to add all contains "abc", "xyz", and so on and if not just show the same name as above.
result should be something like:
|-----|------|
|ABC | 16.00|
|XYZ | 14.00|
|code3| 12.00|
|code7| 43.00|
code8 and so on...
note: included on the quotation will not be included in the required result
im still a newbie on crystal reports..
thank you in advance
grace
Create an IF Then Else formula that returns the value if the condition is true and 0 otherwise.
Then, SUM that formula at whatever level of aggregation (Report-level or Group-level) you need.

Get similar employees based on their attribute values

Consider the following sample table("Customer") with these records
=========
Customer
=========
-----------------------------------------------------------------------------------------------
| customer-id | att-a | att-b | att-c | att-d | att-e | att-f | att-g | att-h | att-i | att-j |
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| customer-1 | att-a-7 | att-b-3 | att-c-10 | att-d-10 | att-e-15 | att-f-11 | att-g-2 | att-h-7 | att-i-5 | att-j-14 |
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| customer-2 | att-a-9 | att-b-7 | att-c-12 | att-d-4 | att-e-10 | att-f-4 | att-g-13 | att-h-4 | att-i-1 | att-j-13 |
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| customer-3 | att-a-10 | att-b-6 | att-c-1 | att-d-1 | att-e-13 | att-f-12 | att-g-9 | att-h-6 | att-i-7 | tt-j-4 |
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| customer-19 | att-a-7 | att-b-9 | att-c-13 | att-d-5 | att-e-8 | att-f-5 | att-g-12 | att-h-14 | att-i-13 | att-j-15 |
--------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
I have these records and many more records dumped into SQL database and wanted to find top 10 similar customer based on the attribute value. For example customer-1 and customer-19 have atleast one column value matching .i.e "att-a-7" so the output should give me 2 customer-id's or top similar customer that are customer-1 and customer-19.
P.S - there can be one or more columns similar across rows.
I'm using windowing technique to find top 10 similar customer and im not sure if I'm correct.
following is my approach I used in my query :
row_number() over (partition by att-a, att-b,..,att-j order by customer-id) as customers
is this correct. ?

Replacing multiple strings from a databsae column with distinct replacements

I have a hive table as below:
+----+---------------+-------------+
| id | name | partnership |
+----+---------------+-------------+
| 1 | sachin sourav | first |
| 2 | sachin sehwag | first |
| 3 | sourav sehwag | first |
| 4 | sachin_sourav | first |
+----+---------------+-------------+
In this table I need to replace strings such as "sachin" with "ST" and "Sourav" with "SG". I am using following query, but it is not solving the purpose.
Query:
select
*,
case
when name regexp('\\bsachin\\b')
then regexp_replace(name,'sachin','ST')
when name regexp('\\bsourav\\b')
then regexp_replace(name,'sourav','SG')
else name
end as newName
from sample1;
Result:
+----+---------------+-------------+---------------+
| id | name | partnership | newname |
+----+---------------+-------------+---------------+
| 4 | sachin_sourav | first | sachin_sourav |
| 3 | sourav sehwag | first | SG sehwag |
| 2 | sachin sehwag | first | ST sehwag |
| 1 | sachin sourav | first | ST sourav |
+----+---------------+-------------+---------------+
Problem: My intention is, when id = 1, the newName column should bring value as "ST SG". I mean it should replace both strings.
You can nest the replaces:
select s.*,
replace(replace(s.name, 'sachin', 'ST'), 'sourav', 'SG') as newName
from sample1 s;
You don't need regular expressions, so just use replace().

Multiple Rows in to One Row

I have a result set in oracle as below: let the table name t1
Name | phase| a_plan | a_actual | b_plan | b_actual | c_plan | c_actual
===================================================================================
RKM | m5-m6| 1/1/2014|1/6/2014 | 2/2/2014 | | 3/3/2014|
RKM | m5-m6| 1/1/2014| | 2/2/2014 | 4/2/2014 | 3/3/2014|
RKM | m5-m6| 1/1/2014| | 2/2/2014 | | 3/3/2014| 5/3/2014
Whereas I need the result set as below in a single row:(a-Plan, b_plan,c_plan will be same across all 3 rows only actual column chnages per row)
Name | phase| a_plan | a_actual | b_plan | b_actual | c_plan | c_actual
===================================================================================
RKM | m5-m6| 1/1/2014|1/6/2014 | 2/2/2014 | 4/2/2014 | 3/3/2014| 5/3/2014
Help in framing the query?
SELECT NAME,
phase,
a_plan,
Max(a_actual),
b_plan,
Max(b_actual),
c_plan,
Max(c_actual)
FROM table
GROUP BY NAME,
phase,
a_plan,
b_plan,
c_plan
/