Reorder array of months - twig

I would like to sort an array of months in the reverse/regular month's order.
At the moment I have the following array
["August", "July", "June", "May", "April", "March", "February", "January", "September"]
I would like to be like
["September", "August", "July", "June", "May", "April", "March", "February", "January"]
I used the sort filter but it sorts in ascending or descending order only.

Related

Merge rows in Power Query to replace null values

I have a table where rows are duplicated and I need to merge these into one row.
Download Example data
In this example I have condensed it to January and February which I need to merge, in my actual data there is one column for each month.
I can do this in Excel but I would like to do it in Power Query instead, if possible?
So far I have tried to Group By and Transposing the rows but either I get an error or I end up back in the same results, one row per month.
Try this on your source data file. It groups on a bunch of stuff, then fills up the month columns
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"ContractNumber", "Year", "Quarter", "Product", "SerialNumber", "PersonFullName", "PersonCostCarrierCode", "CompanyName", "CompanyOrgNo","Cost"}, {{"data", each
Table.FirstN(Table.FillUp(
Table.SelectColumns(_,{"December", "November", "October", "September", "August", "June", "May", "April", "March", "February", "January"})
,{"January", "February", "March", "April", "May", "June", "August", "September", "October", "November", "December"}),1)
, type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"December", "November", "October", "September", "August", "June", "May", "April", "March", "February", "January"}, {"December", "November", "October", "September", "August", "June", "May", "April", "March", "February", "January"})
in #"Expanded data"
You can simply filter the [January] column on "January" and then replace all null values in the [February] column with "February" to get your expected result. All other data is duplicate anyhow, except for the MonthNr, but you are discarding that information.
Here is one way of doing this:
Unpivot the "Months" columns
After doing this, the Attributes and Values columns will be identical, so delete one of them
Group by the columns that define your "duplicate rows"
in the Aggregation, create a Record of the months data:
eg: {"Records", each Record.FromList([Month],[Month]), type record}
Then Expand that record column, using a List of the Months for the Column Names

the sum function cannot work with string values

I'm working with DAX in PowerBI. I hava a column with 80 000 string values.
70% of these values is "European Desk". I want to show this percentage. It's string value, i don't understand how to do it with DAX
Any advice ?
The measure you are looking for is
% European Desk = DIVIDE(
CALCULATE(
COUNT('Table'[String]),
'Table'[String] = "European Desk"
),
COUNT('Table'[String])
)
With CALCULATE you can change the filter context for the COUNT() aggregation.
You can apply this formula to e.g. this table:
Table = DATATABLE(
"Index", INTEGER,
"String", STRING,
{
{1, "European Desk"},
{2, "European Desk"},
{3, "European Desk"},
{4, "African Desk"},
{5, "Asian Desk"}
}
)

Power Query - Combine all lookup values from both tables and create a single column

Apologies if this has been asked before, although I tried searching on the forum and didn't find anything.
Let's say I have two tables having identical columns. The only difference is that these are for two different dates hence the values might change.
Table 1: YDAY
ID
Name
Dept
Salary
Date
X02
Jim
HR
40,000
03/31/2021
X03
Ray
Admin
45,000
03/31/2021
X04
Mark
Sales
55,000
03/31/2021
Table 2: YDAY
ID
Name
Dept
Salary
Date
X01
John
Sales
50,000
03/31/2020
X02
Jim
HR
40,000
03/31/2020
X03
Ray
Admin
45,000
03/31/2020
Now I use Power Query merge request and select ID as the lookup value and perform an outerjoin (i.e. pick up all the ids). However when I do that it will keep two lookup columns separately. What I want to do is merge both of them and create a unified column for ID, which contains all of the IDs in both data sets (i.e. X01 which was present in YDAY but not today and X04 which was present in TODAY but not YDAY). See below for desired result.
Can you please help or point out in the right direction?
My desired result is as follows.
ID
Name
Dept
Salary
Date
Name_Prev
Dept_Prev
Salary_Prev
Date_Prev
X01
John
Sales
50,000
03/31/2020
X02
Jim
HR
40,000
06/30/2021
Jim
HR
40,000
03/31/2020
X03
Ray
Admin
45,000
06/30/2021
Ray
Admin
45,000
03/31/2020
X04
Mark
Sales
55,000
06/30/2021
You could create a consolidated list of IDs from both tables as a separate query, and then merge it with Table1 and Table2 to get your desired output. Not sure if it would be any more efficient but it's an option.
let
#"Table1 IDs" = Table.SelectColumns(Table1,{"ID"}),
#"Table2 IDs" = Table.SelectColumns(Table2,{"ID"}),
#"Appended Query" = Table.Combine({#"Table1 IDs", #"Table2 IDs"}),
#"Removed Duplicates" = Table.Distinct(#"Appended Query"),
#"Merge with Table1" = Table.NestedJoin(#"Removed Duplicates", {"ID"}, Table1, {"ID"}, "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merge with Table1", "Table1", {"Name", "Dept", "Salary", "Date"}, {"Name", "Dept", "Salary", "Date"}),
#"Merge with Table2" = Table.NestedJoin(#"Expanded Table1", {"ID"}, Table2, {"ID"}, "Prev", JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merge with Table2", "Prev", {"Name", "Dept", "Salary", "Date"}, {"Prev.Name", "Prev.Dept", "Prev.Salary", "Prev.Date"}),
#"Sorted Rows" = Table.Sort(#"Expanded Table2",{{"ID", Order.Ascending}})
in
#"Sorted Rows"

Sum values of columns whose names belong to similar category

New in Python - I have a pandas dataframe with 100 rows and 275 columns containing neighborhoods as index and venues as columns. A lot of the venues in the columns are similar and can be grouped under a wider category. The values of the table are frequencies of venues for each neighborhood. I am trying to create a new dataframe with the sums of frequencies of old columns by grouping them under categories.
i.e
df = pd.DataFrame({'Area': ['Area1', 'Area2', 'Area3'],
'Pizza Place': [0.01, 0.02, 0.02],'Sandwich shop': [0.01, 0.02, 0.02],'Burger Joint': [0.01, 0.02, 0.02],'Area': ['Area1', 'Area2', 'Area3'],
'Park': [0.01, 0.02, 0.02],'Elementary School': [0.01, 0.02, 0.02],'Playground': [0.01, 0.02, 0.02]})
I want to create 2 columns that will do something like this:
df['total_fast_food']=sum of frequencies for columns that contain the words:'Pizza','Sandwich','Burger' in their name
df['total_kids]=sum of frequencies for columns that contain the words:'Park','School','Play' in their name
what i tried so far :
df.loc[df['Venue Category'].str.contains('Fast Food|Pizza Place|Burger Joint', case=False)] = 'FastFood'
df_new=df_old.filter(like='Fast',axis=1)
df_new['FastFood'] = df_new.sum(axis=1)
with df.loc I can create the new columns in the existing df and remove the ones used as parameters but in the dataframe the values of the new columns are now all 0.
with filter(like=) i get the sums for all columns that have 'Fast' in their name which is good, but obviously i cannot use it for other parameters i.e 'Joint,Pizza etc'
Any thoughts pls?
In absence of an MCVE which would include input data, an approximate answer can be conceived. Though it is unclear what axis the values are to be counted are on.
Also, category is noted, so a categorical is counted.
import pandas as pd
venue = ["Fast Food", "Pizza Place", "Burger Joint", "Fast Food", "Pizza Place", "Burger Joint", "Burger Joint", "Fast Food", "Fast Food"]
df = pd.DataFrame({"Venue":venue})
df["Venue Category"] = pd.Categorical(df['Venue'])
print(df["Venue Category"].value_counts())

Firebird determine if a string is all numbers

I have a VARCHAR field in a Firebird 2.0 table that can contain alphanumeric characters. I need to sort data on this field, sorting all values that contain only numbers as numbers, and sort all other values as 0.
For example, if I have four values,
"1", "2", "10", "string",
I need to sort it as
"string", "1", "2", "10".
Default sort with string sorts as
"1", "10", "2", "string".
I was thinking of casting the values to INTEGER, but I'm getting conversion error on strings, which is of course correct. How to work around this?
You can use builtin function LPAD:
SELECT
...
<number_field>,
...
FROM
...
ORDER BY
LPAD(<numer_field>, 10)
Create extra column where you store sortable values using your application. Then do sorting based on that column.
If you want your numbers to be at the end then insert "ZZZ" (or "ÜÜÜ" or whatever is last character in your language) in front of numbers. Like Format("ZZZ%012d", my_num);

Resources