if else and while in M language - if-statement

There are 165 Values here and they are Comma Separated. This step is called CommaSeperated
workItemList is a function which takes in the value from CommaSeperated and Brings out the table here
I want to split the 165 items in CommaSeparated into batches of 100 and call workItemList for each batch.
Any ideas on how it must be done?

i have managed to take a comma separated text string and turn it into a grouped table of 100's.
first 100 is called "100" next 100 is called "200" and starts at 101.
enter image description here
here are my steps
enter image description here
First i split the text string into columns using comma as the separator tag, using the for each option.
Then i transposed the whole thing into one column.
Add an index.
Add a modulus of 100
Add a custom column "100counter": = Table.AddColumn(#"Inserted Modulo", "100counter", each if [Modulus]=99 then [Indeks]+1 else null)
forget the renamed columns step :D
used Fill up first, because the to initial 0-100 will have "null" as their modulus.
used Fill down second, because the last records can be null if it doesnt end exactly on 99.
Grouped by "100counter", all rows.
Abbridged code - i cleaned up the 1700 columns in the split by delimiter step :
let
Source = Excel.CurrentWorkbook(){[Name="Tabel2"]}[Content],
#"Split Column by Delimiter" = //Alot of spam code here which is essentially just alll the columns being split. Mark ALL, choose split columns by delimiter, choose comma and all.
#"Transposed Table" = Table.Transpose(#"Split Column by Delimiter"),
#"Added Index" = Table.AddIndexColumn(#"Transposed Table", "Indeks", 0, 1),
#"Inserted Modulo" = Table.AddColumn(#"Added Index", "Modulus", each Number.Mod([Indeks], 100), type number),
#"Added Custom" = Table.AddColumn(#"Inserted Modulo", "100counter", each if [Modulus]=99 then [Indeks]+1 else null),
#"Filled Up" = Table.FillUp(#"Added Custom",{"100counter"}),
#"Filled Down" = Table.FillDown(#"Filled Up",{"100counter"}),
#"Grouped Rows" = Table.Group(#"Filled Down", {"100counter"}, {{"Antal", each _, type table [Column1=number, Indeks=number, Modulus=number, 100counter=number]}})
in
#"Grouped Rows"

Related

vlookup between two worksheets in VBA

i have tried alot doing vlookup between two workbooks, one is open (book1) and the other
needs to be opened by GetOpenFilename() function let us call it (book2).
(book1) contains sheet1 where i want to do the vlookup
(book2) contains sheet2 where i want to match the id (column 1) and get the names (column2) by vlookup.
here is my code. Notice that the number of rows changes every month so, i need to loop until the last row in sheet1(in book1).
Sub status_first_Step()
Range("C:C").Insert
Set book1 = Sheets("Sheet1")
book2 = Application.GetOpenFilename()
Workbooks.Open book2
Set book2 = ActiveWorkbook
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
ws1.Cells(i, "C").Value = Application.VLookup(book1.Cells(i, 1).Value, book2.Sheets("sheet2").Columns("A:Y"), 2, 0)
Next i
End Sub
Thank You.

How to create month table in powerquery (M)

I'm trying to create a month/year table on power query (M). It seems that has no easy way to do that. Here is the code I created to reach that goal.
Its unclear exactly what you are looking for but
let Years = Table.ExpandListColumn(Table.FromRecords({[Years = {1980..2020}]}), "Years"),
#"Add Months" = Table.ExpandListColumn(Table.AddColumn(Years, "Months", each {1..12}), "Months"),
#"Add Month Names" = Table.AddColumn(#"Add Months", "MonthName", each Date.MonthName(#datetime([Years], [Months], 1,0,0,0)))
in #"Add Month Names"
generates a table like this, and you can change the start/ending year in the code
Here is my code. If anybody could find a easier and more elegant solution, it will be welcomed.
To use this code, create a blanked query, go to Advanced Editor and replace the existing code for this one.
let
first_date = #date(2020, 1, 1),
last_day = DateTime.LocalNow(),
num_months = ((Date.Year(last_day) - Date.Year(first_date)) * 12 + Date.Month(last_day) - Date.Month(first_date)),
list_of_num = List.Numbers(0, num_months, 1),
table_from_list = Table.FromValue(list_of_num, [DefaultColumnName = "Index"]),
add_col_year = Table.AddColumn(table_from_list, "Year", each Date.Year(Date.AddMonths(first_date, [Index])), Int64.Type),
add_col_month = Table.AddColumn(add_col_year, "Mes", each Date.Month(Date.AddMonths(first_date, [Index])), Int64.Type)
in
add_col_month

Tag the 6 first wording days of the month

I am trying to find a way to tag the 6 first wording days (Monday to Friday) of the month.
For example, for November I want:
Date
FlagDate
11/01/2021
True
11/02/2021
True
11/03/2021
True
11/04/2021
True
11/05/2021
True
11/06/2021
False
11/07/2021
False
11/08/2021
True
11/09/2021
False
11/10/2021
False
11/11/2021
False
11/12/2021
False
11/13/2021
False
11/14/2021
False
11/15/2021
False
11/16/2021
False
11/17/2021
False
11/18/2021
False
11/19/2021
False
11/20/2021
False
11/21/2021
False
and so on.
Here is M code that adds a column to your existing table.
It assumes that the column Name in your table is Date
It also assumes you have a List of the relevant holiday dates spanning at least the relevant time frame. I obtained the list from https://publicholidays.fr and buffered it to keep it from continuously downloading
See the comments in the M-Code to understand the algorithm
Note that there is no requirement that the Date column encompasses a full month. But if it starts on, for example, the 15th, there will be nothing in that month marked TRUE since the first6 are not there.
Assumption: Date column spans less than 1 year
If that is not the case, will need a different algorithm to create the first6 list.
M Code
//Before adding the "Flag" column enter this code to create a buffered list of working days in the months in the Date column
workingDays =
let
//generate list of all dates from beginning of first month to the last date listed
dts = List.Transform(
{Number.From(Date.StartOfMonth(#"Previous Step"[Date]{0}))..
Number.From(List.Max(#"Previous Step"[Date]))},each Date.From(_)),
//Using List.Generate, create a list of working days from the preceding list
//working days will be days that are Monday-Friday and NOT found in the list of Holiday dates
wdys = List.Generate(
()=>[wd=if Date.DayOfWeek(dts{0})>= Day.Monday
and Date.DayOfWeek(dts{0})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{0}) then dts{0} else null,
idx = 0],
each [idx] < List.Count(dts),
each [wd=if Date.DayOfWeek(dts{[idx]+1})>= Day.Monday
and Date.DayOfWeek(dts{[idx]+1})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{[idx]+1}) then dts{[idx]+1} else null,
idx = [idx]+1],
each [wd]),
//clean the wdys List by removing the Nulls
cleanList = List.RemoveNulls(wdys),
//create a list of the Months within the workday list
monthsInList= List.Distinct(List.Transform(cleanList, each Date.Month(_))),
//Use List.Accumulate to create lists of the first 6 working days in each month
first6 = List.Accumulate(monthsInList,{},
(state, current)=> state &
List.FirstN(List.Select(cleanList,each Date.Month(_)=current),6))
in List.Buffer(first6),
//Now add a column and set the flag if a Date in the date column is found in the first6 list
flagged = Table.AddColumn(#"Previous Step","FlagDate", each List.Contains(workingDays,[Date]), type logical)
in
flagged
filtered to show only dates marked True
M Code Edited to handle spans of more than one year
Change MonthsInList to include the year, and also Test for that in the List.Accumulate step
//Before adding the "Flag" column enter this code to create a buffered list of working days in the months in the Date column
workingDays =
let
//generate list of all dates from beginning of first month to the last date listed
dts = List.Transform(
{Number.From(Date.StartOfMonth(#"Previous Step"[Date]{0}))..
Number.From(List.Max(#"Previous Step"[Date]))},each Date.From(_)),
//Using List.Generate, create a list of working days from the preceding list
//working days will be days that are Monday-Friday and NOT found in the list of Holiday dates
wdys = List.Generate(
()=>[wd=if Date.DayOfWeek(dts{0})>= Day.Monday
and Date.DayOfWeek(dts{0})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{0}) then dts{0} else null,
idx = 0],
each [idx] < List.Count(dts),
each [wd=if Date.DayOfWeek(dts{[idx]+1})>= Day.Monday
and Date.DayOfWeek(dts{[idx]+1})<=Day.Friday
and not List.Contains(#"FR Holidays",dts{[idx]+1}) then dts{[idx]+1} else null,
idx = [idx]+1],
each [wd]),
//clean the wdys List by removing the Nulls
cleanList = List.RemoveNulls(wdys),
//create a list of the Months within the workday list
monthsInList= List.Distinct(List.Transform(cleanList, each Date.ToText(_,"yyyy-MM"))),
//Use List.Accumulate to create lists of the first 6 working days in each month
first6 = List.Accumulate(monthsInList,{},
(state, current)=> state &
List.FirstN(List.Select(cleanList,each Date.ToText(_,"yyyy-MM")=current),6))
in List.Buffer(first6),
//Now add a column and set the flag if a Date in the date column is found in the first6 list
flagged = Table.AddColumn(#"Previous Step","FlagDate", each List.Contains(workingDays,[Date]), type logical)
in
flagged
You can create a custom column with DAX with the following formula
= CONTAINS(TOPN(6,FILTER('Table',WEEKDAY('Table'[Date],2)<6)),'Table'[Date],'Table'[Date])
You can update TopN(6 to accordingly update the number of first working days

PowerBI - Power Query/M - Limit characters on column value and add suffix

Background: I have a column with email subject. I want these to be maximum 30 characters long. For the user to spot that i have cut the ones over 30 characters, i want to add a "..." suffix.
Problem: If the column content is over 30 characters, i want to remove all characters over 30, and add "..." to the end of the string.
What i have tried: I have added the following steps in the Power Query Editor, but it adds "..." to all lines, also the ones under 30 characters.
#"Extracted First Characters" = Table.TransformColumns(#"Duplicated Column", {{"subject - Copy", each Text.Start(_, 30), type text}}),
#"Renamed Columns1" = Table.RenameColumns(#"Extracted First Characters",{{"subject - Copy", "subject - short"}}),
#"Added Suffix" = Table.TransformColumns(#"Renamed Columns1", {{"subject - short", each _ & "...", type text}}),
Thanks in advance
You can transform the subject column, in one step:
= Table.TransformColumns(#"Previous Step", {{"Subject", each if Text.Length(_) > 30 then Text.Start(_, 30) & "..." else _, type text}})
We test if the text length is greater than 30 characters, and if so, return only the first 30 characters suffixed by "...", otherwise just return the text as is.

Prawn Adding new line in table

I have the following code to build PDF document with Prawn:
items = [["PERIOD","EMPLOYEE", "EMPLOYEE NAME", "HOURS", "FTES"]]
items += #mandates.each.map do |mandate|
[
mandate[:fte_period_end_date],
mandate[:fte_employee_id],
strname,
mandate[:fte_sum_of_hours],
mandate[:fte_sum_of_ftes],
]
end
#mandates is sorted by fte_employee_id and fte_by period_end_date
I want to insert totals lines per employe for fte_sum_of_hours and fte_sum_of_ftes when pass throw next employee.
What command permits me to insert these lines with Prawn?
Pass them in the array that you are displaying the total from - in Ruby, calculate for each section of elements, the total. Don't do the work in Prawn (it's not Excel).
data = [["product 1: ","$10.00"],["product 2: ", "$20.00"],["Subtotal:","$30.00]]
For example. Then you can format the table with consideration to row 3, the subtotal, with cell styles.