Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
sdzolin
New Member

Return a value vased on dates

I am very new.  

 

I work on a college campus and I have a form that has a date column.  I want to create a column that converts the date to the semester in which that date occurs.  So for example if the form was completed for January 4, 2024 that would be "Winter 2024"  I am trying to figure out how to specify which dates go to which semester and then have it return the correct value for the date.

1 ACCEPTED SOLUTION

Of course 😉

 

Result

dufoq3_0-1711654050668.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTSNzTVNzIwMlaK1QFzjQyRuPomII4JmGOmb2gG5cUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    Semesters = 
        Table.Buffer(Table.SelectColumns(Table.ExpandListColumn(Table.AddColumn(#table(type table[Month From=Int64.Type, Day From=Int64.Type, Month To=Int64.Type, Day To=Int64.Type, Semester=text], {
                {8,27, 12,19, "Fall"},
                {12,20, 1,20, "Winter"},
                {1,21, 5,17, "Spring"},
                {5,18, 8,26, "Summer"}
                }),
            "DateCode", each 
                [ from = #date(if [Semester]="Winter" then 2019 else 2020, [Month From], [Day From]),
                  to = #date(2020, [Month To], [Day To]),
                  dates = List.Dates(from, Duration.Days(to-from)+1, #duration(1,0,0,0)),
                  dateToNumber = List.Transform(dates, (x)=> Number.From(Date.ToText(x, "MMdd")) )
                ][dateToNumber], type list   
        ), "DateCode"), {"DateCode", "Semester"})),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}}, "en-US"),
    Ad_Semester = Table.AddColumn(ChangedType, "Semester", each 
        [ a = Number.From(Date.ToText([Date], "MMdd")),
          b = Semesters{[DateCode = a]}[Semester],
          c = b & " " & Text.From(if b = "Winter" and a >= 1219 then Date.Year(Date.AddYears([Date], 1)) else Date.Year([Date]) )
        ][c], type text)
in
    Ad_Semester

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

4 REPLIES 4
sdzolin
New Member

How would this work if the crossovers are mid-month.  For example for the 2023-2024 academic year the dates are as follows

 

Fall 2023 = Aug 27 - Dec 19

Winter 2024 = Dec 20 - Jan 20

Spring 2024 = Jan 21- May 17

Summer 2024 = May 18 - Aug 24

You can create a table, and then use that to lookup the relevant date.

In the code below I hard-coded the table, but there are many ways to create it.

 

The table has three columns Semester | dtStart | dtEnd.

 

For example, as a function (named: fnSemester)

(dt as date)=>

let 
    semesterTable= Table.FromRecords(
        {[Semester="Fall 2023", dtStart=#date(2023,8,27), dtEnd = #date(2023,12,19)],
         [Semester="Winter 2024", dtStart=#date(2023,12,20), dtEnd=#date(2024,1,20)],
         [Semester="Spring 2024", dtStart=#date(2024,1,21), dtEnd=#date(2024,5,17)],
         [Semester="Summer 2024", dtStart=#date(2024,5,18), dtEnd=#date(2024,8,24)]},
         type table[Semester=text, dtStart=date, dtEnd=date]),

    pickSemester = Table.AddColumn(semesterTable,"Date Semester", each dt >= [dtStart] and dt <= [dtEnd], type logical)

in 
    try Table.SelectRows(pickSemester, each [Date Semester]=true)[Semester]{0} otherwise "Not in Current Table"

and then, in the main code body

let
    Source = Table.FromColumns(
        {{#date(2023,11,30), #date(2024,1,15),#date(2024,9,20)}},
        type table[Dates=date]
    ),
    #"Invoked Custom Function" = Table.AddColumn(Source, "Semester", each fnSemester([Dates]), type text)
in
    #"Invoked Custom Function"

Result

ronrsnfld_1-1711660882885.png

 

 

 

 

 

Of course 😉

 

Result

dufoq3_0-1711654050668.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTSNzTVNzIwMlaK1QFzjQyRuPomII4JmGOmb2gG5cUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    Semesters = 
        Table.Buffer(Table.SelectColumns(Table.ExpandListColumn(Table.AddColumn(#table(type table[Month From=Int64.Type, Day From=Int64.Type, Month To=Int64.Type, Day To=Int64.Type, Semester=text], {
                {8,27, 12,19, "Fall"},
                {12,20, 1,20, "Winter"},
                {1,21, 5,17, "Spring"},
                {5,18, 8,26, "Summer"}
                }),
            "DateCode", each 
                [ from = #date(if [Semester]="Winter" then 2019 else 2020, [Month From], [Day From]),
                  to = #date(2020, [Month To], [Day To]),
                  dates = List.Dates(from, Duration.Days(to-from)+1, #duration(1,0,0,0)),
                  dateToNumber = List.Transform(dates, (x)=> Number.From(Date.ToText(x, "MMdd")) )
                ][dateToNumber], type list   
        ), "DateCode"), {"DateCode", "Semester"})),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}}, "en-US"),
    Ad_Semester = Table.AddColumn(ChangedType, "Semester", each 
        [ a = Number.From(Date.ToText([Date], "MMdd")),
          b = Semesters{[DateCode = a]}[Semester],
          c = b & " " & Text.From(if b = "Winter" and a >= 1219 then Date.Year(Date.AddYears([Date], 1)) else Date.Year([Date]) )
        ][c], type text)
in
    Ad_Semester

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

dufoq3
Super User
Super User

Hi @sdzolin, try this:

 

Result

dufoq3_0-1711649712344.png

 

If you have different semesters, edit them here:

Left number is semester starting month, right number is semester ending month. Do not overlap months!

dufoq3_1-1711649814672.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTSNzTVNzIwMlaK1QFy9U1AHBMwx0zf0AzKiwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    Semesters = Table.RemoveColumns(Table.ExpandListColumn(Table.AddColumn(#table(type table[Month From=Int64.Type, Month To=Int64.Type, Semester=text], {
        {9, 1, "Winter"},
        {2, 6, "Summer"}
   }), "Months", each if [Month From] > [Month To] then {[Month From]..12} & {1..[Month To]} else {[Month From]..[Month To]}, type list), "Months"), {"Month From", "Month To"}),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}}, "en-US"),
    Ad_Semester = Table.AddColumn(ChangedType, "Semester", each Semesters{[Months = Date.Month([Date])]}[Semester] & " " & Text.From(Date.Year([Date])), type text)
in
    Ad_Semester

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.