Dashboard - Today Date Unless data is blank

1387
16
10-20-2021 12:38 PM
RickeyFight
MVP Regular Contributor

I am looking for help with a script to display only todays data unless todays data has not been displayed yet. If that is the case display yesterday data. 

I am thinking it would be if today > 0 display today data if not display today-1

 

 

16 Replies
jcarlson
MVP Esteemed Contributor

Are you doing this in a data expression? Or working with an existing layer? The former is possible, but the latter probably isn't.

- Josh Carlson
Kendall County GIS
RickeyFight
MVP Regular Contributor

@jcarlson 

I am trying to do this in a data expression. 

0 Kudos
jcarlson
MVP Esteemed Contributor

Ah, well that's not too bad. It's very similar to the example shown here.

// Get layer
var portal = Portal('org-url')
var fs = FeatureSetByPortalItem(
    portal,
    'itemID',
    0, // or whatever the layer index
    [
        'your',
        'fields',
        'some_date_field'
    ],
    false
);

// Get most recent date, convert to SQL-compatible string
var maxDate = Text(Date(fs, 'some_date_field'), 'YYYY-MM-DD');

// Return records for max date
return Filter(fs, "some_date_field = @maxDate")

 

- Josh Carlson
Kendall County GIS
RickeyFight
MVP Regular Contributor

Thanks for the link. 

One issue I have is that I have pre added dates for the year so I cannot pick the max date but maybe I can get it to work with todays date. 

 

jcarlson
MVP Esteemed Contributor

 

// Get layer
var portal = Portal('org-url')
var fs = FeatureSetByPortalItem(
    portal,
    'itemID',
    0, // or whatever the layer index
    [
        'your',
        'fields',
        'some_date_field',
        'some_value_field'
    ],
    false
);

// Filter records with empty values (future dates)
var filt = Filter(fs, "some_value_field IS NOT NULL AND some_value_field <> ''")

// Sort filtered records for most recent first
var sorted = OrderBy(filt, "some_date_field DESC")

// Return top record
return First(sorted)

@RickeyFightyou're right, that was it. Though the AND was probably part of it, too. I've edited this code so that the OrderBy references filt instead of fs.

 

- Josh Carlson
Kendall County GIS
RickeyFight
MVP Regular Contributor

@jcarlson 
I get this now

RickeyFight_0-1634916925938.png

 

0 Kudos
RickeyFight
MVP Regular Contributor

@jcarlson 

 

var portal = Portal("PortalID");
var fs = FeatureSetByPortalItem(
  portal,
  "Numbers",
  1,
  [
    "Date_",
    "W_F_MGD",
    "E_F_MGD",
    "TOTAL_W_E_MGD",
    "TID_FLOW_MGD",
    "REEDER_Percent_FULL",
    "TAP_FLOW_MGD",
    "PLANT_PRODUCTION_MGD",
    "last_edited_date"
  ],
  false
);



// Filter records with empty values (future dates)
var filt = Filter(fs, "REEDER_Percent_FULL IS NOT NULL OR REEDER_Percent_FULL <> ''")

// Sort filtered records for most recent first
var sorted = OrderBy(filt, "Date_ DESC")

// Return top record
return First(sorted)

 

Here is my full code minus the portal login info 

0 Kudos
jcarlson
MVP Esteemed Contributor

Hmmm. Try throwing in some Console commands to output some intermediate things, try to see where it's failing.

After ln 18: Console(fs)
After ln 23: Console(filt)
After ln 26: Console(sorted)
- Josh Carlson
Kendall County GIS
0 Kudos
RickeyFight
MVP Regular Contributor

@jcarlson 
I think you were almost right. 
This is what I got to work 

// Filter records with empty values (future dates)
var filt = Filter(fs, "REEDER_Percent_FULL IS NOT NULL")

// Sort filtered records for most recent first
var sorted = OrderBy(filt, "Date_ DESC")

// Return top record
return First(sorted)

 

If you modify your post I will mark yours as correct