How to get month name from month number in Power BI? - business-intelligence

I have Year number and Month Number in my data.
How using DAX can I get the month name out of month number?
In SSRS its very easy. But how to achieve that using DAX?

You can use:
MonthName = FORMAT(DATE(1, [Num], 1), "MMM")
Result:
Nothing fancy, simply a reconstruction of a fake date from the month number provided, and reformat it with the FORMAT function.
Of course as an alternative you can go the old-fashioned way and write a SWITCH statement and hard-coded for the 12 months. It's up to you.

You can try this too:
Month name = FORMAT('Table'[date_column], "MMM")
If you use single quotes in 'MMM', it doesn't work. Ensure to use ""

By Use of Switch DAX function
MonthName = switch(True(),
MonthID = 1, "jan",MonthID = 2, "Feb",MonthID = 3, "March",MonthID = 4, "April",MonthID = 5, "May",MonthID = 6, "June",MonthID = 7, "july",MonthID = 8, "Aug",MonthID = 9, "Sept",MonthID = 10, "Oct",MonthID = 11, "Nov",MonthID = 12, "Dec"
)

Related

How do I send autosorted blank rows to the bottom?

I have a script that sorts Column B on edit, but there are two problems with it.
1 - It sends the rows with values to the bottom of the sheet.
2 - The numbers do not sort correctly. They should go in the order of 1,3,4,5,and 20, but when it sorts itself, it orders them as 1, 20, 3, 4, 5. It's like it only recognizes the 2 in 20 and places it after 1.
I've searched forum after forum trying to figure this out with no luck so help would be greatly appreciated.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("050")
var range = sheet.getRange("A6:L200");
// Sorts by the values in column 2 (B)
range.sort({column: 2, ascending: true});
}
I don't know if this makes a difference or not, but the sheet that's being sorted uses VLOOKUP. Each Column from B on uses VLOOKUP.
=IFERROR(VLOOKUP(A6,data2019,3,FALSE),"")
First of all, there's no need to get the blank values in the first place, instead you can use getDataRange() to only get the range you need to sort.
Once you've got your range defined, you can sort it. Your values are not being sorted correctly (this is likely due to formatting of the cell from the VLOOKUP). You can simply set the format of the data range to number format using setNumberFormat('0') then sort the data to the order you're expecting.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("050");
var range = sheet.getDataRange();
var numRows = range.getNumRows();
//sets the formatting of column B to a number in all populated rows
sheet.getRange(1, 2, numRows).setNumberFormat('0');
//sorts range by column B using newly formatted values
range.sort({column: 2, ascending: true});
}

Dynamic minimum and maximum dates using M/Power Query

I have a calendar table that is created, on-the-fly, using M.
It starts like this:
let
StartDate = #date(2016, 1, 1),
EndDate = #date(2018, 12, 31),
//Used for 'Offset' Column calculations, you may Hard code CurrentDate for testing e.g. #date(2017,9,1)
CurrentDate = DateTime.Date(DateTime.FixedLocalNow()),
// Specify the last month in your Fiscal Year, e.g. if June is the last month of your Fiscal Year, specify 6
FiscalYearEndMonth = 6,
#"==SET PARAMETERS ABOVE==" = 1,
#"==Build Date Column==" = #"==SET PARAMETERS ABOVE==",
ListDates = List.Dates(StartDate, Number.From(EndDate - StartDate)+1, #duration(1,0,0,0)),
...
...
Is it possible to make the first two lines dynamic so that they pick up minimum and maximum dates from database tables? So these two lines:
let
StartDate = #date(2016, 1, 1),
EndDate = #date(2018, 12, 31),
I have another table being loaded into the model using the following - can I somehow use the Date column from this loads to dynamically set StartDate and EndDate ?
let
Source = Sql.Database("ourServer", "ourDB"),
tb_ModelFact = Source{[Schema="dbo",Item="tb_ModelFact"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(tb_ModelFact,{{"Date", type datetime}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each true),
#"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"Amount", type number}})
in
#"Changed Type1"
Edit
So I tried this
StartDate = List.Min(Fact[Date]),
EndDate = List.Max(Fact[Date]),
....and got this mysterious error?
Expression.Error: We cannot convert the value #datetime(2016, 1, 6, 0, 0, 0) to type Date.
Details:
Value=06/01/2016 00:00:00
Type=Type
Is this error in subsequent M code after the declaration?
Yes. You should be able to write something along these lines:
let
StartDate = List.Min(tb_ModelFact[Date]),
EndDate = List.Max(tb_ModelFact[Date]),
where tb_ModelFact[Date] is the column that has the dates you are trying to take the max and min from.
You will need to change tb_ModelFact to whatever the name of that second query is though.

Python Datetime string parsing: How to know what has been parsed?

I want to use Python to parse a user input string and need to know which portion of date has been specified, e.g.,
"Jan. 2017" => Month = 1, Year = 2017
The result should tell me only month and year are specified in the string, and return those values, while:
"2003-05-01"
specifies day, month and year.
I tried to use dateutil.parser.parse, and gave it a rare default date value. e.g., 1900/01/01, and then compare the parsed result with the default date and see the difference. But if the month or day are both 1 in the parsed result,
it needs one more round of parsing with a different default value, in order to rule out the possibility of it being the default value, or from the user input.
The above way seems quirky. Is there a library for me to parse commonly used date string format and knowing what has been parsed?
I ended up doing the quirky way:
from dateutil.parser import parse
# parse the date str, and return day/month/year specified in the string.
# the value is None if the string does not have information
def parse_date(date_str):
# choose two different dates and see if two parsed results
default_date1 = datetime.datetime(1900, 1, 1, 0, 0)
default_date2 = datetime.datetime(1901, 12, 12, 0, 0)
year = None
month = None
day = None
try:
parsed_result1 = parse(date_str, default=default_date1)
parsed_result2 = parse(date_str, default=default_date2)
if parsed_result1.year == parsed_result2.year: year = parsed_result2.year
if parsed_result1.month == parsed_result2.month: month = parsed_result2.month
if parsed_result1.day == parsed_result2.day: day = parsed_result2.day
return year, month, day
except ValueError:
return None, None, None

Auto sort script for google sheets by 2 columns

I have multiple sheets that need sorting and they need to be sorted by different columns. This is the script I'm currently running:
function onEdit(){
var sheetNames = ["General Clerk Onboarding Tracker"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(function(name) {
var sheet = ss.getSheetByName(name);
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort([{column: 10, ascending: true}]);
});
}
I want to add another range sort column so it'll sort by column 10 first, THEN by column 17. I can't seem to get it to auto-sort 2 columns. Any help would be appreciated!
I think you would just be looking to modify your last line as follows:
range.sort([{column: 10, ascending: true}, {column: 17, ascending: true}]);
I think this might help Additional Sorting Rules have a look and if not I will have a further look into it for you.
Good Luck :)

Linq To Sql - Get week day name from date

Is it possible to construct a query in which I can retrieve week day name from a date into a separate column or variable?
I know I can very easily do this on .NET side but would like it to be in the query.
You can also use SqlFunctions...
var results=context.Listings
.Select(l=>System.Data.Entity.SqlServer.SqlFunctions.DateName("dw",l.modify_date));
Of course, this only works when using a SQL Server. Methods that are cross-database, would be to use EntityFunctions.DateDiff with a known date to get the number of days between whatever and a known prior sunday, then modulus 7, then convert to a string.
It is possible you will need to build your query which returns day names and then join to your result on day number
int[] dayNum={1,2,3,4,5,6,7};
var result = from d in dayNum
let dayOfWeek= (d == 1 ? "Monday" :
d==2 ? "Tuesday" :
d==3 ? "Wednesday" :
d==4 ? "Thursday" :
d==5 ? "Friday" :
d==6 ? "Saturday" :
d==7 ? "Sunday":"")
let dn = d
group d by new {dayOfWeek, dn} into dw
select new { dw.Key.dayOfWeek, dw.Key.dn};
The result of this will be

Resources